Viktor Shin
/
WIZnet-IoTShield-AMM592-SMS
WIZnet-IoTShield-AMM592-SMS example
main.cpp@2:b69c69a16fd2, 2019-09-17 (annotated)
- Committer:
- vikshin
- Date:
- Tue Sep 17 01:50:16 2019 +0000
- Revision:
- 2:b69c69a16fd2
- Parent:
- 1:bdb1c9f07d06
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hkjung | 0:5cd6077e0a8e | 1 | /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED |
hkjung | 0:5cd6077e0a8e | 2 | * Copyright (c) 2019 WIZnet Co., Ltd. |
hkjung | 0:5cd6077e0a8e | 3 | * SPDX-License-Identifier: Apache-2.0 |
hkjung | 0:5cd6077e0a8e | 4 | */ |
hkjung | 0:5cd6077e0a8e | 5 | |
hkjung | 0:5cd6077e0a8e | 6 | /* |
hkjung | 0:5cd6077e0a8e | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hkjung | 0:5cd6077e0a8e | 8 | * you may not use this file except in compliance with the License. |
hkjung | 0:5cd6077e0a8e | 9 | * You may obtain a copy of the License at |
hkjung | 0:5cd6077e0a8e | 10 | * |
hkjung | 0:5cd6077e0a8e | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
hkjung | 0:5cd6077e0a8e | 12 | * |
hkjung | 0:5cd6077e0a8e | 13 | * Unless required by applicable law or agreed to in writing, software |
hkjung | 0:5cd6077e0a8e | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
hkjung | 0:5cd6077e0a8e | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hkjung | 0:5cd6077e0a8e | 16 | * |
hkjung | 0:5cd6077e0a8e | 17 | * See the License for the specific language governing permissions and |
hkjung | 0:5cd6077e0a8e | 18 | * limitations under the License. |
hkjung | 0:5cd6077e0a8e | 19 | * |
hkjung | 0:5cd6077e0a8e | 20 | */ |
hkjung | 0:5cd6077e0a8e | 21 | |
hkjung | 0:5cd6077e0a8e | 22 | |
hkjung | 0:5cd6077e0a8e | 23 | #include <string> |
hkjung | 0:5cd6077e0a8e | 24 | #include "mbed.h" |
hkjung | 0:5cd6077e0a8e | 25 | |
hkjung | 0:5cd6077e0a8e | 26 | #define RET_OK 1 |
hkjung | 0:5cd6077e0a8e | 27 | #define RET_NOK -1 |
hkjung | 0:5cd6077e0a8e | 28 | #define DEBUG_ENABLE 1 |
hkjung | 0:5cd6077e0a8e | 29 | #define DEBUG_DISABLE 0 |
hkjung | 0:5cd6077e0a8e | 30 | #define ON 1 |
hkjung | 0:5cd6077e0a8e | 31 | #define OFF 0 |
hkjung | 0:5cd6077e0a8e | 32 | |
hkjung | 0:5cd6077e0a8e | 33 | #define MAX_BUF_SIZE 1024 |
hkjung | 0:5cd6077e0a8e | 34 | |
vikshin | 2:b69c69a16fd2 | 35 | #define AMM592_APN_PROTOCOL_IPv4 1 |
vikshin | 2:b69c69a16fd2 | 36 | #define AMM592_APN_PROTOCOL_IPv6 2 |
vikshin | 2:b69c69a16fd2 | 37 | #define AMM592_DEFAULT_TIMEOUT 1000 |
vikshin | 2:b69c69a16fd2 | 38 | #define AMM592_CONNECT_TIMEOUT 15000 |
vikshin | 2:b69c69a16fd2 | 39 | #define AMM592_SEND_TIMEOUT 500 |
vikshin | 2:b69c69a16fd2 | 40 | #define AMM592_RECV_TIMEOUT 500 |
hkjung | 0:5cd6077e0a8e | 41 | |
vikshin | 2:b69c69a16fd2 | 42 | #define AMM592_APN_PROTOCOL AMM592_APN_PROTOCOL_IPv6 |
vikshin | 2:b69c69a16fd2 | 43 | #define AMM592_DEFAULT_BAUD_RATE 115200 |
vikshin | 2:b69c69a16fd2 | 44 | #define AMM592_PARSER_DELIMITER "\r\n" |
hkjung | 0:5cd6077e0a8e | 45 | |
hkjung | 0:5cd6077e0a8e | 46 | #define CATM1_APN_SKT "lte-internet.sktelecom.com" |
hkjung | 0:5cd6077e0a8e | 47 | |
vikshin | 2:b69c69a16fd2 | 48 | #define CATM1_DEVICE_NAME_AMM592 "AMM592" |
vikshin | 2:b69c69a16fd2 | 49 | #define DEVNAME CATM1_DEVICE_NAME_AMM592 |
hkjung | 0:5cd6077e0a8e | 50 | |
hkjung | 0:5cd6077e0a8e | 51 | #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } |
hkjung | 0:5cd6077e0a8e | 52 | #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} |
hkjung | 0:5cd6077e0a8e | 53 | |
hkjung | 0:5cd6077e0a8e | 54 | /* Pin configuraiton */ |
hkjung | 0:5cd6077e0a8e | 55 | // Cat.M1 |
hkjung | 0:5cd6077e0a8e | 56 | #define MBED_CONF_IOTSHIELD_CATM1_TX D8 |
hkjung | 0:5cd6077e0a8e | 57 | #define MBED_CONF_IOTSHIELD_CATM1_RX D2 |
hkjung | 1:bdb1c9f07d06 | 58 | #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 |
hkjung | 0:5cd6077e0a8e | 59 | #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 |
hkjung | 1:bdb1c9f07d06 | 60 | |
hkjung | 0:5cd6077e0a8e | 61 | // Sensors |
hkjung | 0:5cd6077e0a8e | 62 | #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 |
hkjung | 0:5cd6077e0a8e | 63 | #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 |
hkjung | 0:5cd6077e0a8e | 64 | |
hkjung | 0:5cd6077e0a8e | 65 | /* Debug message settings */ |
vikshin | 2:b69c69a16fd2 | 66 | #define AMM592_PARSER_DEBUG DEBUG_DISABLE |
hkjung | 0:5cd6077e0a8e | 67 | #define CATM1_DEVICE_DEBUG DEBUG_ENABLE |
hkjung | 0:5cd6077e0a8e | 68 | |
hkjung | 1:bdb1c9f07d06 | 69 | /* SMS */ |
hkjung | 1:bdb1c9f07d06 | 70 | #define SMS_EOF 0x1A |
hkjung | 1:bdb1c9f07d06 | 71 | #define MAX_SMS_SIZE 100 |
hkjung | 1:bdb1c9f07d06 | 72 | |
vikshin | 2:b69c69a16fd2 | 73 | char phone_number[] = "010XXXXXXXX"; |
hkjung | 1:bdb1c9f07d06 | 74 | char send_message[] = "WIZnet Cat.M1 IoT shield is powered on"; |
hkjung | 1:bdb1c9f07d06 | 75 | |
hkjung | 1:bdb1c9f07d06 | 76 | |
hkjung | 0:5cd6077e0a8e | 77 | // Functions: Module Status |
hkjung | 0:5cd6077e0a8e | 78 | void waitCatM1Ready(void); |
vikshin | 2:b69c69a16fd2 | 79 | int8_t setEchoStatus_AMM592(int onoff); |
vikshin | 2:b69c69a16fd2 | 80 | int8_t getUsimStatus_AMM592(void); |
vikshin | 2:b69c69a16fd2 | 81 | int8_t getNetworkStatus_AMM592(void); |
vikshin | 2:b69c69a16fd2 | 82 | int8_t checknSetApn_AMM592(const char * apn); |
vikshin | 2:b69c69a16fd2 | 83 | int8_t getFirmwareVersion_AMM592(char * version); |
hkjung | 0:5cd6077e0a8e | 84 | |
hkjung | 1:bdb1c9f07d06 | 85 | // Functions: SMS |
vikshin | 2:b69c69a16fd2 | 86 | int8_t initSMS_AMM592(void); |
vikshin | 2:b69c69a16fd2 | 87 | int8_t sendSMS_AMM592(char * da, char * msg, int len); |
vikshin | 2:b69c69a16fd2 | 88 | int checkRecvSMS_AMM592(void); |
vikshin | 2:b69c69a16fd2 | 89 | int8_t recvSMS_AMM592(int msg_idx, char * msg, char * da, char * datetime); |
vikshin | 2:b69c69a16fd2 | 90 | int8_t deleteSMS_AMM592(int msg_idx); |
vikshin | 2:b69c69a16fd2 | 91 | int8_t deleteAllSMS_AMM592(int delflag); |
hkjung | 0:5cd6077e0a8e | 92 | |
hkjung | 0:5cd6077e0a8e | 93 | |
hkjung | 0:5cd6077e0a8e | 94 | Serial pc(USBTX, USBRX); // USB debug |
hkjung | 0:5cd6077e0a8e | 95 | |
hkjung | 0:5cd6077e0a8e | 96 | UARTSerial *_serial; // Cat.M1 module |
hkjung | 0:5cd6077e0a8e | 97 | ATCmdParser *_parser; |
hkjung | 0:5cd6077e0a8e | 98 | |
vikshin | 2:b69c69a16fd2 | 99 | DigitalOut _RESET_AMM592(MBED_CONF_IOTSHIELD_CATM1_RESET); |
vikshin | 2:b69c69a16fd2 | 100 | DigitalOut _PWRKEY_AMM592(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); |
hkjung | 0:5cd6077e0a8e | 101 | |
hkjung | 0:5cd6077e0a8e | 102 | void serialPcInit(void) |
hkjung | 0:5cd6077e0a8e | 103 | { |
hkjung | 0:5cd6077e0a8e | 104 | pc.baud(115200); |
hkjung | 0:5cd6077e0a8e | 105 | pc.format(8, Serial::None, 1); |
hkjung | 0:5cd6077e0a8e | 106 | } |
hkjung | 0:5cd6077e0a8e | 107 | |
hkjung | 0:5cd6077e0a8e | 108 | void serialDeviceInit(PinName tx, PinName rx, int baudrate) |
hkjung | 0:5cd6077e0a8e | 109 | { |
hkjung | 0:5cd6077e0a8e | 110 | _serial = new UARTSerial(tx, rx, baudrate); |
hkjung | 0:5cd6077e0a8e | 111 | } |
hkjung | 0:5cd6077e0a8e | 112 | |
hkjung | 0:5cd6077e0a8e | 113 | void serialAtParserInit(const char *delimiter, bool debug_en) |
hkjung | 0:5cd6077e0a8e | 114 | { |
hkjung | 0:5cd6077e0a8e | 115 | _parser = new ATCmdParser(_serial); |
hkjung | 0:5cd6077e0a8e | 116 | _parser->debug_on(debug_en); |
hkjung | 0:5cd6077e0a8e | 117 | _parser->set_delimiter(delimiter); |
vikshin | 2:b69c69a16fd2 | 118 | _parser->set_timeout(AMM592_DEFAULT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 119 | } |
hkjung | 0:5cd6077e0a8e | 120 | |
hkjung | 0:5cd6077e0a8e | 121 | void catm1DeviceInit(void) |
hkjung | 0:5cd6077e0a8e | 122 | { |
hkjung | 0:5cd6077e0a8e | 123 | serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX, |
hkjung | 0:5cd6077e0a8e | 124 | MBED_CONF_IOTSHIELD_CATM1_RX, |
vikshin | 2:b69c69a16fd2 | 125 | AMM592_DEFAULT_BAUD_RATE); |
hkjung | 0:5cd6077e0a8e | 126 | |
vikshin | 2:b69c69a16fd2 | 127 | serialAtParserInit( AMM592_PARSER_DELIMITER, |
vikshin | 2:b69c69a16fd2 | 128 | AMM592_PARSER_DEBUG); |
hkjung | 0:5cd6077e0a8e | 129 | } |
hkjung | 0:5cd6077e0a8e | 130 | |
vikshin | 2:b69c69a16fd2 | 131 | void catm1DeviceReset_AMM592(void) |
hkjung | 1:bdb1c9f07d06 | 132 | { |
vikshin | 2:b69c69a16fd2 | 133 | _RESET_AMM592 = 1; |
vikshin | 2:b69c69a16fd2 | 134 | _PWRKEY_AMM592 = 1; |
hkjung | 1:bdb1c9f07d06 | 135 | wait_ms(300); |
hkjung | 0:5cd6077e0a8e | 136 | |
vikshin | 2:b69c69a16fd2 | 137 | _RESET_AMM592 = 0; |
vikshin | 2:b69c69a16fd2 | 138 | _PWRKEY_AMM592 = 0; |
hkjung | 1:bdb1c9f07d06 | 139 | wait_ms(400); |
hkjung | 0:5cd6077e0a8e | 140 | |
vikshin | 2:b69c69a16fd2 | 141 | _RESET_AMM592 = 1; |
hkjung | 1:bdb1c9f07d06 | 142 | wait_ms(1000); |
hkjung | 0:5cd6077e0a8e | 143 | } |
hkjung | 0:5cd6077e0a8e | 144 | |
hkjung | 1:bdb1c9f07d06 | 145 | |
hkjung | 1:bdb1c9f07d06 | 146 | // ---------------------------------------------------------------- |
hkjung | 1:bdb1c9f07d06 | 147 | // Main routine |
hkjung | 1:bdb1c9f07d06 | 148 | // ---------------------------------------------------------------- |
hkjung | 1:bdb1c9f07d06 | 149 | |
hkjung | 0:5cd6077e0a8e | 150 | int main() |
hkjung | 0:5cd6077e0a8e | 151 | { |
hkjung | 0:5cd6077e0a8e | 152 | serialPcInit(); |
hkjung | 0:5cd6077e0a8e | 153 | catm1DeviceInit(); |
hkjung | 0:5cd6077e0a8e | 154 | |
hkjung | 0:5cd6077e0a8e | 155 | myprintf("Waiting for Cat.M1 Module Ready...\r\n"); |
hkjung | 0:5cd6077e0a8e | 156 | |
vikshin | 2:b69c69a16fd2 | 157 | catm1DeviceReset_AMM592(); |
hkjung | 0:5cd6077e0a8e | 158 | |
hkjung | 0:5cd6077e0a8e | 159 | waitCatM1Ready(); |
hkjung | 1:bdb1c9f07d06 | 160 | |
hkjung | 1:bdb1c9f07d06 | 161 | wait_ms(5000); |
hkjung | 0:5cd6077e0a8e | 162 | |
hkjung | 0:5cd6077e0a8e | 163 | myprintf("System Init Complete\r\n"); |
hkjung | 0:5cd6077e0a8e | 164 | |
hkjung | 0:5cd6077e0a8e | 165 | myprintf("WIZnet IoT Shield for Arm MBED"); |
hkjung | 0:5cd6077e0a8e | 166 | myprintf("LTE Cat.M1 Version"); |
hkjung | 0:5cd6077e0a8e | 167 | myprintf("================================================="); |
vikshin | 2:b69c69a16fd2 | 168 | myprintf(">> Target Board: WIoT-AM01 (AM Telecom AMM592)"); |
hkjung | 1:bdb1c9f07d06 | 169 | myprintf(">> Sample Code: SMS Send & Recv"); |
hkjung | 0:5cd6077e0a8e | 170 | myprintf("=================================================\r\n"); |
hkjung | 0:5cd6077e0a8e | 171 | |
vikshin | 2:b69c69a16fd2 | 172 | setEchoStatus_AMM592(0); |
hkjung | 0:5cd6077e0a8e | 173 | |
vikshin | 2:b69c69a16fd2 | 174 | getUsimStatus_AMM592(); |
hkjung | 0:5cd6077e0a8e | 175 | |
vikshin | 2:b69c69a16fd2 | 176 | getNetworkStatus_AMM592(); |
hkjung | 0:5cd6077e0a8e | 177 | |
vikshin | 2:b69c69a16fd2 | 178 | checknSetApn_AMM592(CATM1_APN_SKT); |
hkjung | 0:5cd6077e0a8e | 179 | |
hkjung | 1:bdb1c9f07d06 | 180 | // SMS configuration |
vikshin | 2:b69c69a16fd2 | 181 | if(initSMS_AMM592() != RET_OK) { |
hkjung | 1:bdb1c9f07d06 | 182 | myprintf("[SMS Init] failed\r\n"); |
hkjung | 1:bdb1c9f07d06 | 183 | while(1){;} |
hkjung | 1:bdb1c9f07d06 | 184 | } |
hkjung | 1:bdb1c9f07d06 | 185 | |
hkjung | 1:bdb1c9f07d06 | 186 | // Send a message |
vikshin | 2:b69c69a16fd2 | 187 | if(sendSMS_AMM592(phone_number, send_message, strlen(send_message)) == RET_OK) { |
vikshin | 2:b69c69a16fd2 | 188 | myprintf("[SMS Sent] to %s, \"%s\"\r\n", phone_number, send_message); |
hkjung | 1:bdb1c9f07d06 | 189 | } |
hkjung | 0:5cd6077e0a8e | 190 | |
hkjung | 1:bdb1c9f07d06 | 191 | #if 0 |
hkjung | 1:bdb1c9f07d06 | 192 | // Delete messages |
vikshin | 2:b69c69a16fd2 | 193 | deleteAllSMS_AMM592(3); |
hkjung | 1:bdb1c9f07d06 | 194 | #endif |
hkjung | 0:5cd6077e0a8e | 195 | |
hkjung | 1:bdb1c9f07d06 | 196 | int msg_idx; |
hkjung | 1:bdb1c9f07d06 | 197 | char recv_message[MAX_SMS_SIZE] = {0, }; |
hkjung | 1:bdb1c9f07d06 | 198 | char dest_addr[20] = {0, }; |
hkjung | 1:bdb1c9f07d06 | 199 | char date_time[25] = {0, }; |
hkjung | 1:bdb1c9f07d06 | 200 | |
hkjung | 1:bdb1c9f07d06 | 201 | _parser->debug_on(DEBUG_DISABLE); |
hkjung | 0:5cd6077e0a8e | 202 | |
hkjung | 1:bdb1c9f07d06 | 203 | while(1) |
hkjung | 1:bdb1c9f07d06 | 204 | { |
hkjung | 1:bdb1c9f07d06 | 205 | // SMS receive check |
vikshin | 2:b69c69a16fd2 | 206 | msg_idx = checkRecvSMS_AMM592(); |
hkjung | 0:5cd6077e0a8e | 207 | |
hkjung | 1:bdb1c9f07d06 | 208 | if(msg_idx > RET_NOK) { // SMS received |
vikshin | 2:b69c69a16fd2 | 209 | _parser->debug_on(AMM592_PARSER_DEBUG); |
hkjung | 1:bdb1c9f07d06 | 210 | |
hkjung | 1:bdb1c9f07d06 | 211 | // Receive a message |
vikshin | 2:b69c69a16fd2 | 212 | if(recvSMS_AMM592(msg_idx, recv_message, dest_addr, date_time) == RET_OK) { |
hkjung | 1:bdb1c9f07d06 | 213 | myprintf("[SMS Recv] from %s, %s, \"%s\"\r\n", dest_addr, date_time, recv_message); |
hkjung | 1:bdb1c9f07d06 | 214 | } |
hkjung | 1:bdb1c9f07d06 | 215 | |
hkjung | 1:bdb1c9f07d06 | 216 | _parser->debug_on(DEBUG_DISABLE); |
hkjung | 1:bdb1c9f07d06 | 217 | } |
hkjung | 1:bdb1c9f07d06 | 218 | } |
hkjung | 0:5cd6077e0a8e | 219 | } |
hkjung | 0:5cd6077e0a8e | 220 | |
hkjung | 1:bdb1c9f07d06 | 221 | |
hkjung | 1:bdb1c9f07d06 | 222 | // ---------------------------------------------------------------- |
hkjung | 1:bdb1c9f07d06 | 223 | // Functions: Cat.M1 Status |
hkjung | 1:bdb1c9f07d06 | 224 | // ---------------------------------------------------------------- |
hkjung | 1:bdb1c9f07d06 | 225 | |
hkjung | 0:5cd6077e0a8e | 226 | void waitCatM1Ready(void) |
hkjung | 0:5cd6077e0a8e | 227 | { |
hkjung | 0:5cd6077e0a8e | 228 | while(1) |
hkjung | 0:5cd6077e0a8e | 229 | { |
vikshin | 2:b69c69a16fd2 | 230 | if(_parser->recv("@NOTI:34,AMT_BOOT_ALERT") && _parser->recv("@NETSTI:3")) |
hkjung | 0:5cd6077e0a8e | 231 | { |
vikshin | 2:b69c69a16fd2 | 232 | myprintf("AMM592 ready\r\n"); |
hkjung | 0:5cd6077e0a8e | 233 | return ; |
hkjung | 0:5cd6077e0a8e | 234 | } |
hkjung | 0:5cd6077e0a8e | 235 | else if(_parser->send("AT") && _parser->recv("OK")) |
hkjung | 0:5cd6077e0a8e | 236 | { |
vikshin | 2:b69c69a16fd2 | 237 | myprintf("AMM592 already available\r\n"); |
hkjung | 0:5cd6077e0a8e | 238 | return ; |
hkjung | 0:5cd6077e0a8e | 239 | } |
hkjung | 0:5cd6077e0a8e | 240 | } |
hkjung | 0:5cd6077e0a8e | 241 | } |
hkjung | 0:5cd6077e0a8e | 242 | |
vikshin | 2:b69c69a16fd2 | 243 | int8_t setEchoStatus_AMM592(int onoff) |
hkjung | 0:5cd6077e0a8e | 244 | { |
hkjung | 0:5cd6077e0a8e | 245 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 246 | char _buf[10]; |
hkjung | 0:5cd6077e0a8e | 247 | |
hkjung | 0:5cd6077e0a8e | 248 | sprintf((char *)_buf, "ATE%d", onoff); |
hkjung | 0:5cd6077e0a8e | 249 | |
hkjung | 0:5cd6077e0a8e | 250 | if(_parser->send(_buf) && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 251 | devlog("Turn Echo %s success\r\n", onoff?"ON":"OFF"); |
hkjung | 0:5cd6077e0a8e | 252 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 253 | } else { |
hkjung | 0:5cd6077e0a8e | 254 | devlog("Turn Echo %s failed\r\n", onoff?"ON":"OFF"); |
hkjung | 0:5cd6077e0a8e | 255 | } |
hkjung | 0:5cd6077e0a8e | 256 | return ret; |
hkjung | 0:5cd6077e0a8e | 257 | } |
hkjung | 0:5cd6077e0a8e | 258 | |
vikshin | 2:b69c69a16fd2 | 259 | int8_t getUsimStatus_AMM592(void) |
hkjung | 0:5cd6077e0a8e | 260 | { |
hkjung | 0:5cd6077e0a8e | 261 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 262 | |
hkjung | 0:5cd6077e0a8e | 263 | _parser->send("AT+CPIN?"); |
hkjung | 0:5cd6077e0a8e | 264 | if(_parser->recv("+CPIN: READY") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 265 | devlog("USIM Status: READY\r\n"); |
hkjung | 0:5cd6077e0a8e | 266 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 267 | } else { |
hkjung | 0:5cd6077e0a8e | 268 | devlog("Retrieving USIM Status failed\r\n"); |
hkjung | 0:5cd6077e0a8e | 269 | } |
hkjung | 0:5cd6077e0a8e | 270 | return ret; |
hkjung | 0:5cd6077e0a8e | 271 | } |
hkjung | 0:5cd6077e0a8e | 272 | |
vikshin | 2:b69c69a16fd2 | 273 | int8_t getNetworkStatus_AMM592(void) |
hkjung | 0:5cd6077e0a8e | 274 | { |
vikshin | 2:b69c69a16fd2 | 275 | int8_t ret = RET_NOK; |
vikshin | 2:b69c69a16fd2 | 276 | bool cereg = false, nsi = false; |
vikshin | 2:b69c69a16fd2 | 277 | int n, stat; |
vikshin | 2:b69c69a16fd2 | 278 | |
vikshin | 2:b69c69a16fd2 | 279 | if(_parser->send("AT+CEREG?") && _parser->recv("+CEREG: %d, %d", &n, &stat) && _parser->recv("OK")){ |
vikshin | 2:b69c69a16fd2 | 280 | if ((n==0 || n==1) && stat==1){ |
vikshin | 2:b69c69a16fd2 | 281 | cereg = true; |
vikshin | 2:b69c69a16fd2 | 282 | } |
vikshin | 2:b69c69a16fd2 | 283 | } |
hkjung | 0:5cd6077e0a8e | 284 | |
vikshin | 2:b69c69a16fd2 | 285 | if(_parser->send("AT@NSI") && _parser->recv("@NSI:%d,\"IN SRV\"") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 286 | devlog("Network Status: attached\r\n"); |
vikshin | 2:b69c69a16fd2 | 287 | nsi = true; |
vikshin | 2:b69c69a16fd2 | 288 | } else if (_parser->send("AT@NSI") && _parser->recv("@NSI: \"LIMITED\"") && _parser->recv("OK")) { |
vikshin | 2:b69c69a16fd2 | 289 | devlog("Network Status: limited\r\n"); |
hkjung | 0:5cd6077e0a8e | 290 | } else { |
hkjung | 0:5cd6077e0a8e | 291 | devlog("Network Status: Error\r\n"); |
hkjung | 0:5cd6077e0a8e | 292 | } |
vikshin | 2:b69c69a16fd2 | 293 | |
vikshin | 2:b69c69a16fd2 | 294 | if (cereg && nsi){ |
vikshin | 2:b69c69a16fd2 | 295 | ret = RET_OK; |
vikshin | 2:b69c69a16fd2 | 296 | } |
hkjung | 0:5cd6077e0a8e | 297 | return ret; |
hkjung | 0:5cd6077e0a8e | 298 | } |
hkjung | 0:5cd6077e0a8e | 299 | |
vikshin | 2:b69c69a16fd2 | 300 | int8_t checknSetApn_AMM592(const char * apn) // Configure Parameters of a TCP/IP Context |
hkjung | 0:5cd6077e0a8e | 301 | { |
vikshin | 2:b69c69a16fd2 | 302 | bool cgdccont = false, cgatt = false; |
hkjung | 0:5cd6077e0a8e | 303 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 304 | |
vikshin | 2:b69c69a16fd2 | 305 | devlog("Checking APN...\r\n"); |
vikshin | 2:b69c69a16fd2 | 306 | //APN setting |
vikshin | 2:b69c69a16fd2 | 307 | if (_parser->send("AT+CGDCONT=1,\"IPV4V6\",\"%s\"", apn) && _parser->recv("OK")){ |
vikshin | 2:b69c69a16fd2 | 308 | cgdccont = true; |
vikshin | 2:b69c69a16fd2 | 309 | } |
vikshin | 2:b69c69a16fd2 | 310 | //Attach setting |
vikshin | 2:b69c69a16fd2 | 311 | if (_parser->send("AT+CGATT?") && _parser->recv("+CGATT: 1") &&_parser->recv("OK")){ |
vikshin | 2:b69c69a16fd2 | 312 | cgatt = true; |
vikshin | 2:b69c69a16fd2 | 313 | } |
hkjung | 0:5cd6077e0a8e | 314 | |
vikshin | 2:b69c69a16fd2 | 315 | if (cgdccont && cgatt){ |
hkjung | 0:5cd6077e0a8e | 316 | ret = RET_OK; |
vikshin | 2:b69c69a16fd2 | 317 | } |
vikshin | 2:b69c69a16fd2 | 318 | |
vikshin | 2:b69c69a16fd2 | 319 | devlog("APN Check Done\r\n"); |
vikshin | 2:b69c69a16fd2 | 320 | |
hkjung | 0:5cd6077e0a8e | 321 | return ret; |
hkjung | 0:5cd6077e0a8e | 322 | } |
hkjung | 0:5cd6077e0a8e | 323 | |
hkjung | 1:bdb1c9f07d06 | 324 | // ---------------------------------------------------------------- |
hkjung | 1:bdb1c9f07d06 | 325 | // Functions: Cat.M1 SMS |
hkjung | 1:bdb1c9f07d06 | 326 | // ---------------------------------------------------------------- |
hkjung | 0:5cd6077e0a8e | 327 | |
vikshin | 2:b69c69a16fd2 | 328 | int8_t initSMS_AMM592(void) |
hkjung | 0:5cd6077e0a8e | 329 | { |
hkjung | 0:5cd6077e0a8e | 330 | int8_t ret = RET_NOK; |
vikshin | 2:b69c69a16fd2 | 331 | bool msgformat = false, charset = false, cnmi = false; |
hkjung | 0:5cd6077e0a8e | 332 | |
hkjung | 1:bdb1c9f07d06 | 333 | // 0 = PDU mode / 1 = Text mode |
hkjung | 1:bdb1c9f07d06 | 334 | if(_parser->send("AT+CMGF=1") && _parser->recv("OK")) { // Set SMS message format as text mode |
hkjung | 1:bdb1c9f07d06 | 335 | devlog("SMS message format: Text mode\r\n"); |
hkjung | 1:bdb1c9f07d06 | 336 | msgformat = true; |
hkjung | 1:bdb1c9f07d06 | 337 | } |
hkjung | 1:bdb1c9f07d06 | 338 | |
hkjung | 1:bdb1c9f07d06 | 339 | // "GSM" / "IRA" / "USC2" |
hkjung | 1:bdb1c9f07d06 | 340 | if(_parser->send("AT+CSCS=\"GSM\"") && _parser->recv("OK")) { // Set character set as GSM |
hkjung | 1:bdb1c9f07d06 | 341 | devlog("SMS character set: GSM\r\n"); |
hkjung | 1:bdb1c9f07d06 | 342 | charset = true; |
hkjung | 1:bdb1c9f07d06 | 343 | } |
hkjung | 1:bdb1c9f07d06 | 344 | |
vikshin | 2:b69c69a16fd2 | 345 | // CNMI setting |
vikshin | 2:b69c69a16fd2 | 346 | if(_parser->send("AT+CNMI=2,1,0,0,0") && _parser->recv("OK")) { // Set CNMI for recv notification |
vikshin | 2:b69c69a16fd2 | 347 | devlog("SMS CNMI set: success\r\n"); |
vikshin | 2:b69c69a16fd2 | 348 | cnmi = true; |
vikshin | 2:b69c69a16fd2 | 349 | } |
vikshin | 2:b69c69a16fd2 | 350 | |
vikshin | 2:b69c69a16fd2 | 351 | if(msgformat && charset && cnmi) { |
hkjung | 0:5cd6077e0a8e | 352 | ret = RET_OK; |
hkjung | 1:bdb1c9f07d06 | 353 | } |
hkjung | 1:bdb1c9f07d06 | 354 | |
hkjung | 1:bdb1c9f07d06 | 355 | return ret; |
hkjung | 0:5cd6077e0a8e | 356 | } |
hkjung | 0:5cd6077e0a8e | 357 | |
vikshin | 2:b69c69a16fd2 | 358 | int8_t sendSMS_AMM592(char * da, char * msg, int len) |
hkjung | 0:5cd6077e0a8e | 359 | { |
hkjung | 0:5cd6077e0a8e | 360 | int8_t ret = RET_NOK; |
hkjung | 1:bdb1c9f07d06 | 361 | int msg_idx = 0; |
hkjung | 0:5cd6077e0a8e | 362 | bool done = false; |
hkjung | 0:5cd6077e0a8e | 363 | |
vikshin | 2:b69c69a16fd2 | 364 | _parser->set_timeout(AMM592_CONNECT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 365 | |
hkjung | 1:bdb1c9f07d06 | 366 | _parser->send("AT+CMGS=\"%s\"", da); // DA(Destination address, Phone number) |
hkjung | 1:bdb1c9f07d06 | 367 | if( !done && _parser->recv(">") ) |
hkjung | 1:bdb1c9f07d06 | 368 | done = (_parser->write(msg, len) <= 0) & _parser->send("%c", SMS_EOF); |
hkjung | 0:5cd6077e0a8e | 369 | |
hkjung | 1:bdb1c9f07d06 | 370 | if( !done ) { |
hkjung | 1:bdb1c9f07d06 | 371 | done = (_parser->recv("+CMGS: %d", &msg_idx) && _parser->recv("OK")); |
hkjung | 1:bdb1c9f07d06 | 372 | if(done) { |
hkjung | 1:bdb1c9f07d06 | 373 | devlog(">> SMS send success: index %d\r\n", msg_idx); |
hkjung | 1:bdb1c9f07d06 | 374 | ret = RET_OK; |
hkjung | 1:bdb1c9f07d06 | 375 | } |
hkjung | 0:5cd6077e0a8e | 376 | } |
vikshin | 2:b69c69a16fd2 | 377 | _parser->set_timeout(AMM592_DEFAULT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 378 | |
hkjung | 0:5cd6077e0a8e | 379 | return ret; |
hkjung | 0:5cd6077e0a8e | 380 | } |
hkjung | 0:5cd6077e0a8e | 381 | |
vikshin | 2:b69c69a16fd2 | 382 | int checkRecvSMS_AMM592(void) |
hkjung | 1:bdb1c9f07d06 | 383 | { |
hkjung | 1:bdb1c9f07d06 | 384 | int ret = RET_NOK; |
hkjung | 1:bdb1c9f07d06 | 385 | int msg_idx = 0; |
hkjung | 0:5cd6077e0a8e | 386 | |
hkjung | 1:bdb1c9f07d06 | 387 | bool received = false; |
hkjung | 0:5cd6077e0a8e | 388 | |
hkjung | 1:bdb1c9f07d06 | 389 | _parser->set_timeout(1); |
hkjung | 1:bdb1c9f07d06 | 390 | received = _parser->recv("+CMTI: \"ME\",%d", &msg_idx); |
vikshin | 2:b69c69a16fd2 | 391 | _parser->set_timeout(AMM592_DEFAULT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 392 | |
hkjung | 1:bdb1c9f07d06 | 393 | if(received) { |
hkjung | 1:bdb1c9f07d06 | 394 | ret = msg_idx; |
hkjung | 1:bdb1c9f07d06 | 395 | devlog("<< SMS received: index %d\r\n", msg_idx); |
hkjung | 1:bdb1c9f07d06 | 396 | } |
hkjung | 0:5cd6077e0a8e | 397 | return ret; |
hkjung | 0:5cd6077e0a8e | 398 | } |
hkjung | 0:5cd6077e0a8e | 399 | |
vikshin | 2:b69c69a16fd2 | 400 | int8_t recvSMS_AMM592(int msg_idx, char * msg, char * da, char * datetime) |
hkjung | 0:5cd6077e0a8e | 401 | { |
hkjung | 1:bdb1c9f07d06 | 402 | int8_t ret = RET_NOK; |
hkjung | 1:bdb1c9f07d06 | 403 | char type[15] = {0, }; |
hkjung | 1:bdb1c9f07d06 | 404 | |
hkjung | 1:bdb1c9f07d06 | 405 | int i = 0; |
hkjung | 1:bdb1c9f07d06 | 406 | bool done = false; |
hkjung | 1:bdb1c9f07d06 | 407 | char recv_msg[MAX_SMS_SIZE] = {0, }; |
hkjung | 1:bdb1c9f07d06 | 408 | char * search_pt; |
hkjung | 1:bdb1c9f07d06 | 409 | Timer t; |
hkjung | 1:bdb1c9f07d06 | 410 | |
hkjung | 1:bdb1c9f07d06 | 411 | memset(recv_msg, 0x00, MAX_SMS_SIZE); |
hkjung | 1:bdb1c9f07d06 | 412 | |
vikshin | 2:b69c69a16fd2 | 413 | _parser->set_timeout(AMM592_RECV_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 414 | |
hkjung | 1:bdb1c9f07d06 | 415 | if( _parser->send("AT+CMGR=%d", msg_idx) |
hkjung | 1:bdb1c9f07d06 | 416 | && _parser->recv("+CMGR: \"%[^\"]\",\"%[^\"]\",,\"%[^\"]\"", type, da, datetime)) |
hkjung | 1:bdb1c9f07d06 | 417 | { |
hkjung | 1:bdb1c9f07d06 | 418 | // timer start |
hkjung | 1:bdb1c9f07d06 | 419 | t.start(); |
hkjung | 1:bdb1c9f07d06 | 420 | |
vikshin | 2:b69c69a16fd2 | 421 | while( !done && (t.read_ms() < AMM592_DEFAULT_TIMEOUT)) { |
hkjung | 1:bdb1c9f07d06 | 422 | _parser->read(&recv_msg[i++], 1); |
hkjung | 1:bdb1c9f07d06 | 423 | search_pt = strstr(recv_msg, "OK"); |
hkjung | 1:bdb1c9f07d06 | 424 | if (search_pt != 0) { |
hkjung | 1:bdb1c9f07d06 | 425 | done = true; // break; |
hkjung | 1:bdb1c9f07d06 | 426 | } |
hkjung | 1:bdb1c9f07d06 | 427 | } |
hkjung | 1:bdb1c9f07d06 | 428 | if(i > 8) { |
hkjung | 1:bdb1c9f07d06 | 429 | memcpy(msg, recv_msg+2, i-8); |
hkjung | 1:bdb1c9f07d06 | 430 | devlog("<< SMS receive success: index %d\r\n", msg_idx); |
hkjung | 1:bdb1c9f07d06 | 431 | ret = RET_OK; |
hkjung | 1:bdb1c9f07d06 | 432 | } |
hkjung | 1:bdb1c9f07d06 | 433 | } |
vikshin | 2:b69c69a16fd2 | 434 | _parser->set_timeout(AMM592_DEFAULT_TIMEOUT); |
hkjung | 1:bdb1c9f07d06 | 435 | _parser->flush(); |
hkjung | 1:bdb1c9f07d06 | 436 | |
hkjung | 0:5cd6077e0a8e | 437 | return ret; |
hkjung | 0:5cd6077e0a8e | 438 | } |
hkjung | 0:5cd6077e0a8e | 439 | |
vikshin | 2:b69c69a16fd2 | 440 | int8_t deleteSMS_AMM592(int msg_idx) |
hkjung | 0:5cd6077e0a8e | 441 | { |
hkjung | 0:5cd6077e0a8e | 442 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 443 | |
hkjung | 1:bdb1c9f07d06 | 444 | if(_parser->send("AT+CMGD=%d", msg_idx) && _parser->recv("OK")) |
hkjung | 1:bdb1c9f07d06 | 445 | { |
hkjung | 1:bdb1c9f07d06 | 446 | devlog("Message index[%d] has been deleted\r\n", msg_idx); |
hkjung | 1:bdb1c9f07d06 | 447 | ret = RET_OK; |
hkjung | 1:bdb1c9f07d06 | 448 | } |
hkjung | 0:5cd6077e0a8e | 449 | return ret; |
hkjung | 0:5cd6077e0a8e | 450 | } |
hkjung | 1:bdb1c9f07d06 | 451 | |
vikshin | 2:b69c69a16fd2 | 452 | int8_t deleteAllSMS_AMM592(int delflag) |
hkjung | 1:bdb1c9f07d06 | 453 | { |
hkjung | 1:bdb1c9f07d06 | 454 | int8_t ret = RET_NOK; |
hkjung | 1:bdb1c9f07d06 | 455 | |
hkjung | 1:bdb1c9f07d06 | 456 | if(_parser->send("AT+CMGD=0,%d", delflag) && _parser->recv("OK")) |
hkjung | 1:bdb1c9f07d06 | 457 | { |
hkjung | 1:bdb1c9f07d06 | 458 | devlog("All messages has been deleted (delflag: %d)\r\n", delflag); |
hkjung | 1:bdb1c9f07d06 | 459 | ret = RET_OK; |
hkjung | 1:bdb1c9f07d06 | 460 | } |
hkjung | 1:bdb1c9f07d06 | 461 | return ret; |
vikshin | 2:b69c69a16fd2 | 462 | } |