|
20 | 20 | #include "string.h" |
21 | 21 | #include "ctype.h" |
22 | 22 |
|
23 | | -#define linux_baud 250000 |
| 23 | +#define LINUX_BAUD_DEFAULT 250000 |
| 24 | +#define LINUX_BAUD_LININO 115200 |
24 | 25 | #define SHADOW_TOPIC_FIXED_LEN 32 // strlen("$aws/things/") + strlen("/shadow/update/delta") |
25 | 26 | #define RETURN_KEY 13 // ASCII code for '\r' |
26 | 27 | #define NEXTLINE_KEY 10 // ASCII code for '\n' |
| 28 | +#define MAX_NUM_PARA 7 // Maximum number of parameters in protocol communication |
27 | 29 |
|
28 | 30 | const char* OUT_OF_BUFFER_ERR_MSG = "OUT OF BUFFER SIZE"; |
29 | 31 |
|
30 | | -IoT_Error_t aws_iot_mqtt_client::setup(char* client_id, bool clean_session, MQTTv_t MQTT_version) { |
| 32 | +// Choose different baudrate for different version of openWRT OS |
| 33 | +Baud_t aws_iot_mqtt_client::find_baud_type() { |
| 34 | + Baud_t rc_type = BAUD_TYPE_UNKNOWN; |
| 35 | + // 1st attempt |
| 36 | + Serial1.begin(LINUX_BAUD_DEFAULT); |
| 37 | + while(!Serial1); |
| 38 | + exec_cmd("\n", true, false); // jump over the welcoming prompt for Open WRT |
| 39 | + delay(1000); // in case this is the first boot-up |
| 40 | + int i; |
| 41 | + for(i = 0; i < MAX_NUM_PARA + 1; i++) { |
| 42 | + // exit the previous python process and jump over the half-baked protocol communication |
| 43 | + exec_cmd("~\n", true, false); |
| 44 | + } |
| 45 | + delay(1500); // delay 1500 ms for all related python script to exit |
| 46 | + |
| 47 | + exec_cmd("uname\n", true, false); // check OS version |
| 48 | + if(strncmp(rw_buf, "Linux", 5) != 0) { // Not an Arduino? |
| 49 | + Serial1.begin(LINUX_BAUD_LININO); |
| 50 | + while(!Serial1); |
| 51 | + exec_cmd("\n", true, false); // jump over the welcoming prompt for Open WRT |
| 52 | + delay(1000); // in case this is the first boot-up |
| 53 | + int i; |
| 54 | + for(i = 0; i < MAX_NUM_PARA + 1; i++) { |
| 55 | + // exit the previous python process and jump over all possible half-baked protocol communication |
| 56 | + exec_cmd("~\n", true, false); |
| 57 | + } |
| 58 | + delay(1500); // delay 1500 ms for all related python script to exit |
| 59 | + |
| 60 | + exec_cmd("uname\n", true, false); // check OS version |
| 61 | + if(strncmp(rw_buf, "Linux", 5) != 0) { |
| 62 | + // No more board types to try |
| 63 | + } |
| 64 | + else {rc_type = BAUD_TYPE_LININO;} |
| 65 | + } |
| 66 | + else {rc_type = BAUD_TYPE_ARDUINO;} |
| 67 | + |
| 68 | + return rc_type; |
| 69 | +} |
| 70 | + |
| 71 | +IoT_Error_t aws_iot_mqtt_client::setup_exec(char* client_id, bool clean_session, MQTTv_t MQTT_version) { |
| 72 | + // Serial1 is started before this call |
31 | 73 | IoT_Error_t rc = NONE_ERROR; |
32 | | - if(client_id == NULL) {rc = NULL_VALUE_ERROR;} |
33 | | - else if(strlen(client_id) >= MAX_BUF_SIZE) {rc = OVERFLOW_ERROR;} |
34 | | - else { |
35 | | - my_client_id = client_id; |
36 | | - // Start Serial1 |
37 | | - Serial1.begin(linux_baud); |
38 | | - while(!Serial1); // blocking until Serial1 is ready |
| 74 | + exec_cmd("cd /root\n", false, false); |
| 75 | + exec_cmd("python aws_iot_mqtt_client.py\n", false, false); |
39 | 76 |
|
40 | | - exec_cmd("cd .\n", false, false); // placeholder: jump over the welcoming prompt for Open WRT |
41 | | - exec_cmd("cd /root\n", false, false); |
42 | | - exec_cmd("~\n", true, false); // exit the previous python process |
43 | | - delay(1000); // delay 1000 ms for all related python script to exit |
44 | | - exec_cmd("python aws_iot_mqtt_client.py\n", false, false); |
| 77 | + // Create obj |
| 78 | + exec_cmd("i\n", false, false); |
45 | 79 |
|
46 | | - // Create obj |
47 | | - exec_cmd("i\n", false, false); |
| 80 | + sprintf(rw_buf, "%s\n", client_id); |
| 81 | + exec_cmd(rw_buf, false, false); |
48 | 82 |
|
49 | | - sprintf(rw_buf, "%s\n", client_id); |
50 | | - exec_cmd(rw_buf, false, false); |
| 83 | + int num_temp = clean_session ? 1 : 0; |
| 84 | + sprintf(rw_buf, "%d\n", num_temp); |
| 85 | + exec_cmd(rw_buf, false, false); |
51 | 86 |
|
52 | | - int num_temp = clean_session ? 1 : 0; |
53 | | - sprintf(rw_buf, "%d\n", num_temp); |
54 | | - exec_cmd(rw_buf, false, false); |
| 87 | + sprintf(rw_buf, "%u\n", MQTT_version); |
| 88 | + exec_cmd(rw_buf, true, false); |
55 | 89 |
|
56 | | - sprintf(rw_buf, "%u\n", MQTT_version); |
57 | | - exec_cmd(rw_buf, true, false); |
| 90 | + if(strncmp(rw_buf, "I T", 3) != 0) {rc = SET_UP_ERROR;} |
58 | 91 |
|
59 | | - if(strncmp(rw_buf, "I T", 3) != 0) {rc = SET_UP_ERROR;} |
| 92 | + return rc; |
| 93 | +} |
| 94 | + |
| 95 | +IoT_Error_t aws_iot_mqtt_client::setup(char* client_id, bool clean_session, MQTTv_t MQTT_version) { |
| 96 | + IoT_Error_t rc = NONE_ERROR; |
| 97 | + if(client_id == NULL) {rc = NULL_VALUE_ERROR;} |
| 98 | + else if(strlen(client_id) >= MAX_BUF_SIZE) {rc = OVERFLOW_ERROR;} |
| 99 | + // No input error below this line |
| 100 | + else { |
| 101 | + my_client_id = client_id; |
| 102 | + Baud_t baud_type = find_baud_type(); // Find out baud type |
| 103 | + // Communication failed due to baud rate issue |
| 104 | + if(BAUD_TYPE_UNKNOWN == baud_type) {rc = SERIAL1_COMMUNICATION_ERROR;} |
| 105 | + else { |
| 106 | + rc = setup_exec(client_id, clean_session, MQTT_version); |
| 107 | + } |
60 | 108 | } |
61 | 109 |
|
62 | 110 | return rc; |
|
0 commit comments