Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@23:0a48eebaaba8, 2014-03-24 (annotated)
- Committer:
- vwochnik
- Date:
- Mon Mar 24 09:58:46 2014 +0000
- Revision:
- 23:0a48eebaaba8
- Parent:
- 20:ef9cc1b42e9d
- Child:
- 24:8f9b678a7932
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vwochnik | 6:642e7c233e83 | 1 | #include "common.h" |
vwochnik | 23:0a48eebaaba8 | 2 | #include "apndb.h" |
vwochnik | 15:0ccf0f530a05 | 3 | #include "io.h" |
vwochnik | 6:642e7c233e83 | 4 | #include "ATResultBuffer.h" |
vwochnik | 0:ed4d6fd405ea | 5 | |
vwochnik | 0:ed4d6fd405ea | 6 | C027 c027; |
vwochnik | 6:642e7c233e83 | 7 | ATCommandsInterface *cmdIface = NULL; |
vwochnik | 6:642e7c233e83 | 8 | char cIMEI[21] = ""; |
vwochnik | 23:0a48eebaaba8 | 9 | char cIMSI[16] = ""; |
vwochnik | 6:642e7c233e83 | 10 | char cCellId[9] = ""; |
vwochnik | 7:f77afd49c35d | 11 | char cICCID[24] = ""; |
vwochnik | 6:642e7c233e83 | 12 | sigq_t sigQ = {0, 0}; |
vwochnik | 0:ed4d6fd405ea | 13 | |
vwochnik | 0:ed4d6fd405ea | 14 | int main() |
vwochnik | 0:ed4d6fd405ea | 15 | { |
vwochnik | 7:f77afd49c35d | 16 | int ret; |
vwochnik | 7:f77afd49c35d | 17 | |
vwochnik | 7:f77afd49c35d | 18 | puts("Started..."); |
vwochnik | 15:0ccf0f530a05 | 19 | io_init(); |
vwochnik | 15:0ccf0f530a05 | 20 | puts("IO initialized."); |
vwochnik | 15:0ccf0f530a05 | 21 | |
vwochnik | 7:f77afd49c35d | 22 | c027.mdmWakeup(); |
vwochnik | 7:f77afd49c35d | 23 | c027.mdmReset(); |
vwochnik | 7:f77afd49c35d | 24 | c027.mdmPower(true); |
vwochnik | 7:f77afd49c35d | 25 | UbloxUSBGSMModem modem; |
vwochnik | 7:f77afd49c35d | 26 | cmdIface = modem.getATCommandsInterface(); |
vwochnik | 6:642e7c233e83 | 27 | |
vwochnik | 6:642e7c233e83 | 28 | puts("Connecting..."); |
vwochnik | 23:0a48eebaaba8 | 29 | if (modem.connect("")) { |
vwochnik | 23:0a48eebaaba8 | 30 | puts("Searching for login..."); |
vwochnik | 23:0a48eebaaba8 | 31 | // get imsi number |
vwochnik | 23:0a48eebaaba8 | 32 | printf("IMSI: %s\n", imsi()); |
vwochnik | 23:0a48eebaaba8 | 33 | apndb_t *apn = apndb_get(imsi()); |
vwochnik | 23:0a48eebaaba8 | 34 | if (apn == NULL) |
vwochnik | 23:0a48eebaaba8 | 35 | puts("No APN found."); |
vwochnik | 23:0a48eebaaba8 | 36 | else |
vwochnik | 23:0a48eebaaba8 | 37 | printf("APN: %s\n", apn->apn); |
vwochnik | 23:0a48eebaaba8 | 38 | |
vwochnik | 23:0a48eebaaba8 | 39 | if (modem.connect("public4.m2minternet.com")) { |
vwochnik | 23:0a48eebaaba8 | 40 | puts("GPRS connection failure."); |
vwochnik | 23:0a48eebaaba8 | 41 | return 2; |
vwochnik | 23:0a48eebaaba8 | 42 | } |
vwochnik | 23:0a48eebaaba8 | 43 | printf("IMSI: %s\n", imsi()); |
vwochnik | 23:0a48eebaaba8 | 44 | apn = apndb_get(imsi()); |
vwochnik | 23:0a48eebaaba8 | 45 | if (apn == NULL) |
vwochnik | 23:0a48eebaaba8 | 46 | puts("No APN found."); |
vwochnik | 23:0a48eebaaba8 | 47 | else |
vwochnik | 23:0a48eebaaba8 | 48 | printf("APN: %s\n", apn->apn); |
vwochnik | 4:363b4cc49445 | 49 | } |
vwochnik | 5:dee05a7c70f9 | 50 | |
vwochnik | 8:940c782eec5e | 51 | // get before starting program as they're permanent |
vwochnik | 13:4045b6ac448d | 52 | if ((!strlen(imei())) || (!strlen(iccid()))) { |
vwochnik | 12:beb64aa0da86 | 53 | puts("Couldn't read IMEI or ICCID."); |
vwochnik | 12:beb64aa0da86 | 54 | return 2; |
vwochnik | 12:beb64aa0da86 | 55 | } |
vwochnik | 23:0a48eebaaba8 | 56 | |
vwochnik | 23:0a48eebaaba8 | 57 | // Shown on display |
vwochnik | 23:0a48eebaaba8 | 58 | signalQuality(); |
vwochnik | 4:363b4cc49445 | 59 | |
vwochnik | 7:f77afd49c35d | 60 | puts("Starting program..."); |
vwochnik | 8:940c782eec5e | 61 | |
vwochnik | 8:940c782eec5e | 62 | // restart program upon failure |
vwochnik | 8:940c782eec5e | 63 | do { |
vwochnik | 8:940c782eec5e | 64 | ret = program(); |
vwochnik | 12:beb64aa0da86 | 65 | if (ret) |
vwochnik | 12:beb64aa0da86 | 66 | puts("Restarting program..."); |
vwochnik | 8:940c782eec5e | 67 | } while (ret); |
vwochnik | 4:363b4cc49445 | 68 | |
vwochnik | 4:363b4cc49445 | 69 | modem.disconnect(); |
vwochnik | 6:642e7c233e83 | 70 | c027.mdmPower(false); |
vwochnik | 6:642e7c233e83 | 71 | |
vwochnik | 8:940c782eec5e | 72 | return 0; |
vwochnik | 4:363b4cc49445 | 73 | } |
vwochnik | 4:363b4cc49445 | 74 | |
vwochnik | 6:642e7c233e83 | 75 | const char * imei() |
vwochnik | 4:363b4cc49445 | 76 | { |
vwochnik | 6:642e7c233e83 | 77 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 78 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 79 | |
vwochnik | 6:642e7c233e83 | 80 | if ((cmdIface == NULL) || (strlen(cIMEI))) |
vwochnik | 6:642e7c233e83 | 81 | return cIMEI; |
vwochnik | 4:363b4cc49445 | 82 | |
vwochnik | 6:642e7c233e83 | 83 | cmdIface->execute("AT+CGSN", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 84 | if (ATCommandsInterface::ATResult::AT_OK == result.result) |
vwochnik | 6:642e7c233e83 | 85 | strcpy(cIMEI, buffer.getLastLine()); |
vwochnik | 6:642e7c233e83 | 86 | else |
vwochnik | 6:642e7c233e83 | 87 | cIMEI[0] = '\0'; |
vwochnik | 4:363b4cc49445 | 88 | |
vwochnik | 6:642e7c233e83 | 89 | return cIMEI; |
vwochnik | 0:ed4d6fd405ea | 90 | } |
vwochnik | 4:363b4cc49445 | 91 | |
vwochnik | 23:0a48eebaaba8 | 92 | const char * imsi() |
vwochnik | 23:0a48eebaaba8 | 93 | { |
vwochnik | 23:0a48eebaaba8 | 94 | ATCommandsInterface::ATResult result; |
vwochnik | 23:0a48eebaaba8 | 95 | ATResultBuffer buffer; |
vwochnik | 23:0a48eebaaba8 | 96 | |
vwochnik | 23:0a48eebaaba8 | 97 | if ((cmdIface == NULL) || (strlen(cIMSI))) |
vwochnik | 23:0a48eebaaba8 | 98 | return cIMSI; |
vwochnik | 23:0a48eebaaba8 | 99 | |
vwochnik | 23:0a48eebaaba8 | 100 | cmdIface->execute("AT+CIMI", &buffer, &result); |
vwochnik | 23:0a48eebaaba8 | 101 | if (ATCommandsInterface::ATResult::AT_OK == result.result) |
vwochnik | 23:0a48eebaaba8 | 102 | strcpy(cIMSI, buffer.getLastLine()); |
vwochnik | 23:0a48eebaaba8 | 103 | else |
vwochnik | 23:0a48eebaaba8 | 104 | cIMSI[0] = '\0'; |
vwochnik | 23:0a48eebaaba8 | 105 | |
vwochnik | 23:0a48eebaaba8 | 106 | return cIMSI; |
vwochnik | 23:0a48eebaaba8 | 107 | } |
vwochnik | 23:0a48eebaaba8 | 108 | |
vwochnik | 6:642e7c233e83 | 109 | const char * cellId() |
vwochnik | 4:363b4cc49445 | 110 | { |
vwochnik | 6:642e7c233e83 | 111 | int len; uint32_t cellId; |
vwochnik | 6:642e7c233e83 | 112 | |
vwochnik | 6:642e7c233e83 | 113 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 114 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 115 | |
vwochnik | 6:642e7c233e83 | 116 | cCellId[0] = '\0'; |
vwochnik | 4:363b4cc49445 | 117 | |
vwochnik | 6:642e7c233e83 | 118 | if (cmdIface == NULL) |
vwochnik | 6:642e7c233e83 | 119 | return cCellId; |
vwochnik | 4:363b4cc49445 | 120 | |
vwochnik | 6:642e7c233e83 | 121 | cmdIface->executeSimple("AT+CREG=2", &result); |
vwochnik | 6:642e7c233e83 | 122 | if (ATCommandsInterface::ATResult::AT_OK != result.result) |
vwochnik | 6:642e7c233e83 | 123 | return cCellId; |
vwochnik | 4:363b4cc49445 | 124 | |
vwochnik | 6:642e7c233e83 | 125 | cmdIface->execute("AT+CREG?", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 126 | if (buffer.getLastLine() == NULL) |
vwochnik | 6:642e7c233e83 | 127 | return cCellId; |
vwochnik | 6:642e7c233e83 | 128 | sscanf(buffer.getLastLine(), "+CREG: 2,%*d,\"%*lx\",\"%lx\",%*d%n", &cellId, &len); |
vwochnik | 6:642e7c233e83 | 129 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 6:642e7c233e83 | 130 | return cCellId; |
vwochnik | 6:642e7c233e83 | 131 | snprintf(cCellId, sizeof(cCellId), "%lX", cellId); |
vwochnik | 4:363b4cc49445 | 132 | |
vwochnik | 6:642e7c233e83 | 133 | return cCellId; |
vwochnik | 4:363b4cc49445 | 134 | } |
vwochnik | 4:363b4cc49445 | 135 | |
vwochnik | 7:f77afd49c35d | 136 | const char * iccid() |
vwochnik | 7:f77afd49c35d | 137 | { |
vwochnik | 7:f77afd49c35d | 138 | int len; |
vwochnik | 7:f77afd49c35d | 139 | |
vwochnik | 7:f77afd49c35d | 140 | ATCommandsInterface::ATResult result; |
vwochnik | 7:f77afd49c35d | 141 | ATResultBuffer buffer; |
vwochnik | 7:f77afd49c35d | 142 | |
vwochnik | 7:f77afd49c35d | 143 | if ((cmdIface == NULL) || (strlen(cICCID))) |
vwochnik | 7:f77afd49c35d | 144 | return cICCID; |
vwochnik | 7:f77afd49c35d | 145 | |
vwochnik | 7:f77afd49c35d | 146 | cmdIface->execute("AT+CCID?", &buffer, &result); |
vwochnik | 7:f77afd49c35d | 147 | if (buffer.getLastLine() == NULL) |
vwochnik | 7:f77afd49c35d | 148 | return cCellId; |
vwochnik | 7:f77afd49c35d | 149 | sscanf(buffer.getLastLine(), "+CCID: %s%n", cICCID, &len); |
vwochnik | 7:f77afd49c35d | 150 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 7:f77afd49c35d | 151 | cICCID[0] = '\0'; |
vwochnik | 7:f77afd49c35d | 152 | |
vwochnik | 7:f77afd49c35d | 153 | return cICCID; |
vwochnik | 7:f77afd49c35d | 154 | } |
vwochnik | 7:f77afd49c35d | 155 | |
vwochnik | 6:642e7c233e83 | 156 | sigq_t * signalQuality() |
vwochnik | 4:363b4cc49445 | 157 | { |
vwochnik | 6:642e7c233e83 | 158 | int rssi = 0, ber = 0, len = 0; |
vwochnik | 4:363b4cc49445 | 159 | |
vwochnik | 6:642e7c233e83 | 160 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 161 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 162 | |
vwochnik | 6:642e7c233e83 | 163 | sigQ.rssi = 0; sigQ.ber = 0; |
vwochnik | 20:ef9cc1b42e9d | 164 | |
vwochnik | 20:ef9cc1b42e9d | 165 | lcd_signal(0, 0); |
vwochnik | 4:363b4cc49445 | 166 | |
vwochnik | 6:642e7c233e83 | 167 | if (cmdIface == NULL) |
vwochnik | 6:642e7c233e83 | 168 | return &sigQ; |
vwochnik | 4:363b4cc49445 | 169 | |
vwochnik | 6:642e7c233e83 | 170 | cmdIface->execute("AT+CSQ", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 171 | if (buffer.getLastLine() == NULL) |
vwochnik | 6:642e7c233e83 | 172 | return &sigQ; |
vwochnik | 6:642e7c233e83 | 173 | |
vwochnik | 6:642e7c233e83 | 174 | sscanf(buffer.getLastLine(), "+CSQ: %d,%d%n", &rssi, &ber, &len); |
vwochnik | 6:642e7c233e83 | 175 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 6:642e7c233e83 | 176 | return &sigQ; |
vwochnik | 6:642e7c233e83 | 177 | |
vwochnik | 6:642e7c233e83 | 178 | if (rssi <= 31) |
vwochnik | 6:642e7c233e83 | 179 | sigQ.rssi = -113 + (2 * rssi); |
vwochnik | 6:642e7c233e83 | 180 | else |
vwochnik | 6:642e7c233e83 | 181 | sigQ.rssi = 0; |
vwochnik | 4:363b4cc49445 | 182 | |
vwochnik | 6:642e7c233e83 | 183 | switch (ber) { |
vwochnik | 6:642e7c233e83 | 184 | case 0: sigQ.ber = 49; break; |
vwochnik | 6:642e7c233e83 | 185 | case 1: sigQ.ber = 43; break; |
vwochnik | 6:642e7c233e83 | 186 | case 2: sigQ.ber = 37; break; |
vwochnik | 6:642e7c233e83 | 187 | case 3: sigQ.ber = 31; break; |
vwochnik | 6:642e7c233e83 | 188 | case 4: sigQ.ber = 25; break; |
vwochnik | 6:642e7c233e83 | 189 | case 5: sigQ.ber = 19; break; |
vwochnik | 6:642e7c233e83 | 190 | case 6: sigQ.ber = 13; break; |
vwochnik | 6:642e7c233e83 | 191 | case 7: sigQ.ber = 7; break; |
vwochnik | 6:642e7c233e83 | 192 | default: sigQ.ber = 0; |
vwochnik | 4:363b4cc49445 | 193 | } |
vwochnik | 20:ef9cc1b42e9d | 194 | |
vwochnik | 20:ef9cc1b42e9d | 195 | lcd_signal(sigQ.rssi, sigQ.ber); |
vwochnik | 4:363b4cc49445 | 196 | |
vwochnik | 6:642e7c233e83 | 197 | return &sigQ; |
vwochnik | 4:363b4cc49445 | 198 | } |