aboutsummaryrefslogtreecommitdiffstats
path: root/imraa/imraa_arduino.c
blob: f1e2bf572024fcf744332b5093700f4d0b8ddd32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
 * Author: Longwei Su <lsu@ics.com>
 * Copyright (c) 2015-2016 Intel Corporation.
 *
 * SPDX-License-Identifier: MIT
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <json-c/json.h>
#include <libudev.h>

#include <argp.h>
#include <error.h>

#include <mraa/uart.h>
#include <mraa/gpio.h>
#include <mraa/i2c.h>
#include <mraa/pwm.h>

#include <mraa_internal.h>
#include <imraa_lock.h>

const char*
imraa_get_101_serialport()
{
    struct udev* udev;
    struct udev_enumerate* enumerate;
    struct udev_list_entry *devices, *dev_list_entry;
    struct udev_device* dev;
    const char* ret = NULL;
    udev = udev_new();
    if (!udev) {
        printf("Can't create udev, check libudev\n");
        exit(1);
    }
    enumerate = udev_enumerate_new(udev);
    udev_enumerate_add_match_subsystem(enumerate, "tty");
    udev_enumerate_add_match_property(enumerate, "ID_VENDOR_ID", "8087");
    udev_enumerate_add_match_property(enumerate, "ID_MODEL_ID", "0ab6");
    // udev_enumerate_add_match_property(enumerate, "ID_SERIAL",
    // "Intel_ARDUINO_101_AE6642SQ55000RS");
    udev_enumerate_scan_devices(enumerate);
    devices = udev_enumerate_get_list_entry(enumerate);

    udev_list_entry_foreach(dev_list_entry, devices)
    {
        const char* path;
        path = udev_list_entry_get_name(dev_list_entry);
        dev = udev_device_new_from_syspath(udev, path);
        ret = udev_device_get_devnode(dev);
    }
    udev_enumerate_unref(enumerate);
    udev_unref(udev);

    if (ret) {
        printf("Ardunio 101 Device Node Path: %s\n", ret);
    } else {
        printf("Can't detect any Ardunio 101 Device on tty\n");
    }
    return ret;
}

mraa_result_t
imraa_reset_arduino(const char* modem)
{
    mraa_uart_context uart;
    uart = mraa_uart_init_raw(modem);
    mraa_uart_set_baudrate(uart, 1200);

    if (uart == NULL) {
        fprintf(stderr, "UART failed to setup\n");
        return MRAA_ERROR_UNSPECIFIED;
    }
    mraa_uart_stop(uart);
    mraa_deinit();
    return MRAA_SUCCESS;
}

int
imraa_flash_101(const char* bin_path, const char* bin_file_name, const char* tty)
{
    if (imraa_reset_arduino(tty) != MRAA_SUCCESS) {
        fprintf(stderr, "Failed to reset arduino on %s for unknown reason, carrying on...\n", tty);
    }

    char* ln = NULL;
    size_t len = 0;
    bool board_found = false;
    const char* dfu_list = "/dfu-util -d ,8087:0ABA -l";
    size_t bin_path_len = strlen(bin_path);

    char* full_dfu_list = (char*) calloc((bin_path_len + strlen(dfu_list) + 1), sizeof(char));
    if (!full_dfu_list) {
        printf("imraa_flash_101 can't allocate string buffer for dfu list\n");
        exit(1);
    }
    strncat(full_dfu_list, bin_path, strlen(bin_path));
    strncat(full_dfu_list, dfu_list, strlen(dfu_list));

    int i;
    // dfu list is still needed, as the time for reset and recognized is varies from platform to
    // platform.
    // once dfu able to query available devices, then it is ready to flash
    for (i = 0; i < 10 && board_found == false; i++) {
        printf("Waiting for device...\n");
        // dfu-util -d,8087:0ABA -l
        FILE* dfu_result = popen(full_dfu_list, "r");
        if (dfu_result == NULL) {
            printf("Failed to run command\n");
            exit(1);
        }

        if (i == 4) {
            printf("Flashing is taking longer than expected\n");
            printf("Try pressing MASTER_RESET button\n");
        }

        while (getline(&ln, &len, dfu_result) != -1) {
            if (strstr(ln, "sensor_core")) {
                board_found = true;
                printf("Device found!\n");
                break;
            }
        }
        sleep(1);
        if (pclose(dfu_result) != 0) {
            printf("Failed to close command\n");
            exit(1);
        }
    }
    free(ln);

    if (board_found == false) {
        printf("ERROR: Device is not responding.\n");
        exit(1);
    }

    const char* dfu_upload = "/dfu-util -d ,8087:0ABA -D ";
    const char* dfu_option = " -v -a 7 -R";
    int buffersize = bin_path_len + strlen(dfu_upload) + strlen(bin_file_name) + strlen(dfu_option) + 1;
    char* full_dfu_upload = calloc(buffersize, sizeof(char));
    if (!full_dfu_upload) {
        printf("imraa_flash_101 can't allocate string buffer for dfu flash\n");
        exit(1);
    }
    strncat(full_dfu_upload, bin_path, strlen(bin_path));
    strncat(full_dfu_upload, dfu_upload, strlen(dfu_upload));
    strncat(full_dfu_upload, bin_file_name, strlen(bin_file_name));
    strncat(full_dfu_upload, dfu_option, strlen(dfu_option));
    printf("flash cmd: %s\n", full_dfu_upload);
    int status = system(full_dfu_upload);
    if (status != 0) {
        printf("ERROR: Upload failed on %s\n", tty);
        exit(1);
    }
    printf("SUCCESS: Sketch will execute in about 5 seconds.\n");
    free(full_dfu_list);
    free(full_dfu_upload);
    sleep(5);
    return 0;
}

void
imraa_handle_subplatform(struct json_object* jobj, bool force_update)
{
    struct json_object* platform;
    int i, ionum;
    const char* dfu_loc = NULL;
    const char* lockfile_loc = NULL;
    const char* flash_loc = NULL;
    const char* usbserial = NULL;

    struct json_object* dfu_location;
    if (json_object_object_get_ex(jobj, "dfu-utils-location", &dfu_location) == true) {
        if (json_object_is_type(dfu_location, json_type_string)) {
            printf("dfu location: %s\n", json_object_get_string(dfu_location));
            dfu_loc = json_object_get_string(dfu_location);
        } else {
            fprintf(stderr, "dfu location string incorrectly parsed\n");
        }
    }

    struct json_object* lockfile_location;
    if (json_object_object_get_ex(jobj, "lockfile-location", &lockfile_location) == true) {
        if (json_object_is_type(lockfile_location, json_type_string)) {
            printf("lock file location: %s\n", json_object_get_string(lockfile_location));
            lockfile_loc = json_object_get_string(lockfile_location);
        } else {
            fprintf(stderr, "lock file string incorrectly parsed\n");
        }
    }

    if (json_object_object_get_ex(jobj, "Platform", &platform) == true) {
        if (json_object_is_type(platform, json_type_array)) {
            ionum = json_object_array_length(platform);
            for (i = 0; i < ionum; i++) {
                struct json_object* ioobj = json_object_array_get_idx(platform, i);
                json_object_object_foreach(ioobj, key, val)
                {
                    if (strcmp(key, "flash") == 0) {
                        flash_loc = json_object_get_string(val);
                    }
                    if (strcmp(key, "usbserial") == 0) {
                        usbserial = json_object_get_string(val);
                    }
                }
            }
        } else {
            fprintf(stderr, "platform string incorrectly parsed\n");
        }
    }
    if (flash_loc == NULL || usbserial == NULL ) {
        printf("conf didn't give image location or usb serial, skip flashing\n");
        return;
    }
    if (!dfu_loc) {
        dfu_loc = "/usr/bin";
        printf("No dfu path found, using default path /usr/bin instead");
    }
    if (!lockfile_loc) {
        lockfile_loc = "/tmp/imraa.lock";
        printf("No lock path found, using default lock file /tmp/imraa.lock instead");
    }

    // got flash? do flash
    if (access(lockfile_loc, F_OK) != -1 && force_update == false) {
        printf("already exist a lock file, skip flashing\n");
        printf("force upgrade? remove the lockfile or run with force\n");
        return;
    } else {
        fprintf(stdout, "Starting to flash board\n");
        if (force_update) {
            fprintf(stdout, "**Caution: force update mode**\n");
        }

        // TODO flash img checksum, and serialport validation?
        const char* detected_serialport = imraa_get_101_serialport();
        if (detected_serialport == NULL) {
            printf("No subplatform detected, check USB connection, skip flashing\n");
            return;
        }
        if (strcmp(usbserial, "auto") != 0 && strcmp(usbserial, detected_serialport) != 0) {
            printf("given serial port didn't match detected serial port, skip flashing\n");
            return;
        }
        if (imraa_flash_101(dfu_loc, flash_loc, detected_serialport) == 0) {
            imraa_write_lockfile(lockfile_loc, detected_serialport);
        } else {
            fprintf(stderr, "invalid flashing paramenters, please check agian\n");
            fprintf(stderr, "DFU Util location: %s\n", dfu_loc);
            fprintf(stderr, "Flash Img location: %s\n", dfu_loc);
            fprintf(stderr, "USB Serial: %s\n", usbserial);
            fprintf(stderr, "Flash failed, push master reset and try again\n");
        }
    }
}