WIZnet-IoTShield-AMM592-SMS example

Committer:
hkjung
Date:
Wed Mar 20 05:36:45 2019 +0000
Revision:
1:bdb1c9f07d06
Parent:
0:5cd6077e0a8e
Child:
2:b69c69a16fd2
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew 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
hkjung 0:5cd6077e0a8e 35 #define BG96_APN_PROTOCOL_IPv4 1
hkjung 0:5cd6077e0a8e 36 #define BG96_APN_PROTOCOL_IPv6 2
hkjung 0:5cd6077e0a8e 37 #define BG96_DEFAULT_TIMEOUT 1000
hkjung 0:5cd6077e0a8e 38 #define BG96_CONNECT_TIMEOUT 15000
hkjung 0:5cd6077e0a8e 39 #define BG96_SEND_TIMEOUT 500
hkjung 0:5cd6077e0a8e 40 #define BG96_RECV_TIMEOUT 500
hkjung 0:5cd6077e0a8e 41
hkjung 0:5cd6077e0a8e 42 #define BG96_APN_PROTOCOL BG96_APN_PROTOCOL_IPv6
hkjung 0:5cd6077e0a8e 43 #define BG96_DEFAULT_BAUD_RATE 115200
hkjung 0:5cd6077e0a8e 44 #define BG96_PARSER_DELIMITER "\r\n"
hkjung 0:5cd6077e0a8e 45
hkjung 0:5cd6077e0a8e 46 #define CATM1_APN_SKT "lte-internet.sktelecom.com"
hkjung 0:5cd6077e0a8e 47
hkjung 0:5cd6077e0a8e 48 #define CATM1_DEVICE_NAME_BG96 "BG96"
hkjung 0:5cd6077e0a8e 49 #define DEVNAME CATM1_DEVICE_NAME_BG96
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 */
hkjung 0:5cd6077e0a8e 66 #define BG96_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
hkjung 1:bdb1c9f07d06 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);
hkjung 0:5cd6077e0a8e 79 int8_t setEchoStatus_BG96(bool onoff);
hkjung 0:5cd6077e0a8e 80 int8_t getUsimStatus_BG96(void);
hkjung 0:5cd6077e0a8e 81 int8_t getNetworkStatus_BG96(void);
hkjung 0:5cd6077e0a8e 82 int8_t checknSetApn_BG96(const char * apn);
hkjung 0:5cd6077e0a8e 83 int8_t getFirmwareVersion_BG96(char * version);
hkjung 0:5cd6077e0a8e 84
hkjung 1:bdb1c9f07d06 85 // Functions: SMS
hkjung 1:bdb1c9f07d06 86 int8_t initSMS_BG96(void);
hkjung 1:bdb1c9f07d06 87 int8_t sendSMS_BG96(char * da, char * msg, int len);
hkjung 1:bdb1c9f07d06 88 int checkRecvSMS_BG96(void);
hkjung 1:bdb1c9f07d06 89 int8_t recvSMS_BG96(int msg_idx, char * msg, char * da, char * datetime);
hkjung 1:bdb1c9f07d06 90 int8_t deleteSMS_BG96(int msg_idx);
hkjung 1:bdb1c9f07d06 91 int8_t deleteAllSMS_BG96(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
hkjung 1:bdb1c9f07d06 99 DigitalOut _RESET_BG96(MBED_CONF_IOTSHIELD_CATM1_RESET);
hkjung 1:bdb1c9f07d06 100 DigitalOut _PWRKEY_BG96(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);
hkjung 0:5cd6077e0a8e 118 _parser->set_timeout(BG96_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,
hkjung 0:5cd6077e0a8e 125 BG96_DEFAULT_BAUD_RATE);
hkjung 0:5cd6077e0a8e 126
hkjung 0:5cd6077e0a8e 127 serialAtParserInit( BG96_PARSER_DELIMITER,
hkjung 0:5cd6077e0a8e 128 BG96_PARSER_DEBUG);
hkjung 0:5cd6077e0a8e 129 }
hkjung 0:5cd6077e0a8e 130
hkjung 1:bdb1c9f07d06 131 void catm1DeviceReset_BG96(void)
hkjung 1:bdb1c9f07d06 132 {
hkjung 1:bdb1c9f07d06 133 _RESET_BG96 = 1;
hkjung 1:bdb1c9f07d06 134 _PWRKEY_BG96 = 1;
hkjung 1:bdb1c9f07d06 135 wait_ms(300);
hkjung 0:5cd6077e0a8e 136
hkjung 1:bdb1c9f07d06 137 _RESET_BG96 = 0;
hkjung 1:bdb1c9f07d06 138 _PWRKEY_BG96 = 0;
hkjung 1:bdb1c9f07d06 139 wait_ms(400);
hkjung 0:5cd6077e0a8e 140
hkjung 1:bdb1c9f07d06 141 _RESET_BG96 = 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
hkjung 1:bdb1c9f07d06 157 catm1DeviceReset_BG96();
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("=================================================");
hkjung 0:5cd6077e0a8e 168 myprintf(">> Target Board: WIoT-QC01 (Quectel BG96)");
hkjung 1:bdb1c9f07d06 169 myprintf(">> Sample Code: SMS Send & Recv");
hkjung 0:5cd6077e0a8e 170 myprintf("=================================================\r\n");
hkjung 0:5cd6077e0a8e 171
hkjung 0:5cd6077e0a8e 172 setEchoStatus_BG96(OFF);
hkjung 0:5cd6077e0a8e 173
hkjung 0:5cd6077e0a8e 174 getUsimStatus_BG96();
hkjung 0:5cd6077e0a8e 175
hkjung 0:5cd6077e0a8e 176 getNetworkStatus_BG96();
hkjung 0:5cd6077e0a8e 177
hkjung 0:5cd6077e0a8e 178 checknSetApn_BG96(CATM1_APN_SKT);
hkjung 0:5cd6077e0a8e 179
hkjung 1:bdb1c9f07d06 180 // SMS configuration
hkjung 1:bdb1c9f07d06 181 if(initSMS_BG96() != 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
hkjung 1:bdb1c9f07d06 187 if(sendSMS_BG96(phone_number, send_message, strlen(send_message)) == RET_OK) {
hkjung 1:bdb1c9f07d06 188 myprintf("[SMS Send] 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
hkjung 1:bdb1c9f07d06 193 deleteAllSMS_BG96(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
hkjung 1:bdb1c9f07d06 206 msg_idx = checkRecvSMS_BG96();
hkjung 0:5cd6077e0a8e 207
hkjung 1:bdb1c9f07d06 208 if(msg_idx > RET_NOK) { // SMS received
hkjung 1:bdb1c9f07d06 209 _parser->debug_on(BG96_PARSER_DEBUG);
hkjung 1:bdb1c9f07d06 210
hkjung 1:bdb1c9f07d06 211 // Receive a message
hkjung 1:bdb1c9f07d06 212 if(recvSMS_BG96(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 {
hkjung 0:5cd6077e0a8e 230 if(_parser->recv("RDY"))
hkjung 0:5cd6077e0a8e 231 {
hkjung 0:5cd6077e0a8e 232 myprintf("BG96 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 {
hkjung 0:5cd6077e0a8e 237 myprintf("BG96 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
hkjung 0:5cd6077e0a8e 243 int8_t setEchoStatus_BG96(bool 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
hkjung 0:5cd6077e0a8e 259 int8_t getUsimStatus_BG96(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
hkjung 0:5cd6077e0a8e 273 int8_t getNetworkStatus_BG96(void)
hkjung 0:5cd6077e0a8e 274 {
hkjung 0:5cd6077e0a8e 275 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 276
hkjung 0:5cd6077e0a8e 277 if(_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"SRV\"") && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 278 devlog("Network Status: attached\r\n");
hkjung 0:5cd6077e0a8e 279 ret = RET_OK;
hkjung 0:5cd6077e0a8e 280 } else if (_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"LIMITED\"") && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 281 devlog("Network Status: limited\r\n");
hkjung 0:5cd6077e0a8e 282 ret = RET_OK;
hkjung 0:5cd6077e0a8e 283 } else {
hkjung 0:5cd6077e0a8e 284 devlog("Network Status: Error\r\n");
hkjung 0:5cd6077e0a8e 285 }
hkjung 0:5cd6077e0a8e 286 return ret;
hkjung 0:5cd6077e0a8e 287 }
hkjung 0:5cd6077e0a8e 288
hkjung 0:5cd6077e0a8e 289 int8_t checknSetApn_BG96(const char * apn) // Configure Parameters of a TCP/IP Context
hkjung 0:5cd6077e0a8e 290 {
hkjung 0:5cd6077e0a8e 291 char resp_str[100];
hkjung 0:5cd6077e0a8e 292
hkjung 0:5cd6077e0a8e 293 uint16_t i = 0;
hkjung 0:5cd6077e0a8e 294 char * search_pt;
hkjung 0:5cd6077e0a8e 295
hkjung 0:5cd6077e0a8e 296 memset(resp_str, 0, sizeof(resp_str));
hkjung 0:5cd6077e0a8e 297
hkjung 0:5cd6077e0a8e 298 devlog("Checking APN...\r\n");
hkjung 0:5cd6077e0a8e 299
hkjung 0:5cd6077e0a8e 300 _parser->send("AT+QICSGP=1");
hkjung 0:5cd6077e0a8e 301
hkjung 0:5cd6077e0a8e 302 while(1)
hkjung 0:5cd6077e0a8e 303 {
hkjung 0:5cd6077e0a8e 304 _parser->read(&resp_str[i++], 1);
hkjung 0:5cd6077e0a8e 305 search_pt = strstr(resp_str, "OK\r\n");
hkjung 0:5cd6077e0a8e 306 if (search_pt != 0)
hkjung 0:5cd6077e0a8e 307 {
hkjung 0:5cd6077e0a8e 308 break;
hkjung 0:5cd6077e0a8e 309 }
hkjung 0:5cd6077e0a8e 310 }
hkjung 0:5cd6077e0a8e 311
hkjung 0:5cd6077e0a8e 312 search_pt = strstr(resp_str, apn);
hkjung 0:5cd6077e0a8e 313 if (search_pt == 0)
hkjung 0:5cd6077e0a8e 314 {
hkjung 0:5cd6077e0a8e 315 devlog("Mismatched APN: %s\r\n", resp_str);
hkjung 0:5cd6077e0a8e 316 devlog("Storing APN %s...\r\n", apn);
hkjung 0:5cd6077e0a8e 317 if(!(_parser->send("AT+QICSGP=1,%d,\"%s\",\"\",\"\",0", BG96_APN_PROTOCOL, apn) && _parser->recv("OK")))
hkjung 0:5cd6077e0a8e 318 {
hkjung 0:5cd6077e0a8e 319 return RET_NOK; // failed
hkjung 0:5cd6077e0a8e 320 }
hkjung 0:5cd6077e0a8e 321 }
hkjung 1:bdb1c9f07d06 322 devlog("APN Check Done\r\n");
hkjung 1:bdb1c9f07d06 323
hkjung 0:5cd6077e0a8e 324 return RET_OK;
hkjung 0:5cd6077e0a8e 325 }
hkjung 0:5cd6077e0a8e 326
hkjung 0:5cd6077e0a8e 327 int8_t getFirmwareVersion_BG96(char * version)
hkjung 0:5cd6077e0a8e 328 {
hkjung 0:5cd6077e0a8e 329 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 330
hkjung 0:5cd6077e0a8e 331 if(_parser->send("AT+QGMR") && _parser->recv("%s\n", version) && _parser->recv("OK"))
hkjung 0:5cd6077e0a8e 332 {
hkjung 0:5cd6077e0a8e 333 ret = RET_OK;
hkjung 0:5cd6077e0a8e 334 }
hkjung 0:5cd6077e0a8e 335 return ret;
hkjung 0:5cd6077e0a8e 336 }
hkjung 0:5cd6077e0a8e 337
hkjung 0:5cd6077e0a8e 338 int8_t getImeiNumber_BG96(char * imei)
hkjung 0:5cd6077e0a8e 339 {
hkjung 0:5cd6077e0a8e 340 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 341
hkjung 0:5cd6077e0a8e 342 if(_parser->send("AT+CGSN") && _parser->recv("%s\n", imei) && _parser->recv("OK"))
hkjung 0:5cd6077e0a8e 343 {
hkjung 0:5cd6077e0a8e 344 ret = RET_OK;
hkjung 0:5cd6077e0a8e 345 }
hkjung 0:5cd6077e0a8e 346 return ret;
hkjung 0:5cd6077e0a8e 347 }
hkjung 0:5cd6077e0a8e 348
hkjung 1:bdb1c9f07d06 349 // ----------------------------------------------------------------
hkjung 1:bdb1c9f07d06 350 // Functions: Cat.M1 DNS
hkjung 1:bdb1c9f07d06 351 // ----------------------------------------------------------------
hkjung 1:bdb1c9f07d06 352
hkjung 1:bdb1c9f07d06 353
hkjung 0:5cd6077e0a8e 354 int8_t getIpAddressByName_BG96(const char * name, char * ipstr)
hkjung 0:5cd6077e0a8e 355 {
hkjung 0:5cd6077e0a8e 356 char buf2[50];
hkjung 0:5cd6077e0a8e 357 bool ok;
hkjung 0:5cd6077e0a8e 358 int err, ipcount, dnsttl;
hkjung 0:5cd6077e0a8e 359
hkjung 0:5cd6077e0a8e 360 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 361
hkjung 0:5cd6077e0a8e 362 ok = ( _parser->send("AT+QIDNSGIP=1,\"%s\"", name)
hkjung 0:5cd6077e0a8e 363 && _parser->recv("OK")
hkjung 0:5cd6077e0a8e 364 && _parser->recv("+QIURC: \"dnsgip\",%d,%d,%d", &err, &ipcount, &dnsttl)
hkjung 0:5cd6077e0a8e 365 && err==0
hkjung 0:5cd6077e0a8e 366 && ipcount > 0
hkjung 0:5cd6077e0a8e 367 );
hkjung 0:5cd6077e0a8e 368
hkjung 0:5cd6077e0a8e 369 if( ok ) {
hkjung 0:5cd6077e0a8e 370 _parser->recv("+QIURC: \"dnsgip\",\"%[^\"]\"", ipstr); //use the first DNS value
hkjung 0:5cd6077e0a8e 371 for( int i=0; i<ipcount-1; i++ )
hkjung 0:5cd6077e0a8e 372 _parser->recv("+QIURC: \"dnsgip\",\"%[^\"]\"", buf2); //and discrard the rest if >1
hkjung 0:5cd6077e0a8e 373
hkjung 0:5cd6077e0a8e 374 ret = RET_OK;
hkjung 0:5cd6077e0a8e 375 }
hkjung 0:5cd6077e0a8e 376 return ret;
hkjung 0:5cd6077e0a8e 377 }
hkjung 0:5cd6077e0a8e 378
hkjung 0:5cd6077e0a8e 379
hkjung 1:bdb1c9f07d06 380 // ----------------------------------------------------------------
hkjung 1:bdb1c9f07d06 381 // Functions: Cat.M1 SMS
hkjung 1:bdb1c9f07d06 382 // ----------------------------------------------------------------
hkjung 0:5cd6077e0a8e 383
hkjung 1:bdb1c9f07d06 384 int8_t initSMS_BG96(void)
hkjung 0:5cd6077e0a8e 385 {
hkjung 0:5cd6077e0a8e 386 int8_t ret = RET_NOK;
hkjung 1:bdb1c9f07d06 387 bool msgformat = false, charset = false;
hkjung 0:5cd6077e0a8e 388
hkjung 1:bdb1c9f07d06 389 // 0 = PDU mode / 1 = Text mode
hkjung 1:bdb1c9f07d06 390 if(_parser->send("AT+CMGF=1") && _parser->recv("OK")) { // Set SMS message format as text mode
hkjung 1:bdb1c9f07d06 391 devlog("SMS message format: Text mode\r\n");
hkjung 1:bdb1c9f07d06 392 msgformat = true;
hkjung 1:bdb1c9f07d06 393 }
hkjung 1:bdb1c9f07d06 394
hkjung 1:bdb1c9f07d06 395 // "GSM" / "IRA" / "USC2"
hkjung 1:bdb1c9f07d06 396 if(_parser->send("AT+CSCS=\"GSM\"") && _parser->recv("OK")) { // Set character set as GSM
hkjung 1:bdb1c9f07d06 397 devlog("SMS character set: GSM\r\n");
hkjung 1:bdb1c9f07d06 398 charset = true;
hkjung 1:bdb1c9f07d06 399 }
hkjung 1:bdb1c9f07d06 400
hkjung 1:bdb1c9f07d06 401 if(msgformat && charset) {
hkjung 0:5cd6077e0a8e 402 ret = RET_OK;
hkjung 1:bdb1c9f07d06 403 }
hkjung 1:bdb1c9f07d06 404
hkjung 1:bdb1c9f07d06 405 return ret;
hkjung 0:5cd6077e0a8e 406 }
hkjung 0:5cd6077e0a8e 407
hkjung 1:bdb1c9f07d06 408 int8_t sendSMS_BG96(char * da, char * msg, int len)
hkjung 0:5cd6077e0a8e 409 {
hkjung 0:5cd6077e0a8e 410 int8_t ret = RET_NOK;
hkjung 1:bdb1c9f07d06 411 int msg_idx = 0;
hkjung 0:5cd6077e0a8e 412 bool done = false;
hkjung 0:5cd6077e0a8e 413
hkjung 0:5cd6077e0a8e 414 _parser->set_timeout(BG96_CONNECT_TIMEOUT);
hkjung 0:5cd6077e0a8e 415
hkjung 1:bdb1c9f07d06 416 _parser->send("AT+CMGS=\"%s\"", da); // DA(Destination address, Phone number)
hkjung 1:bdb1c9f07d06 417 if( !done && _parser->recv(">") )
hkjung 1:bdb1c9f07d06 418 done = (_parser->write(msg, len) <= 0) & _parser->send("%c", SMS_EOF);
hkjung 0:5cd6077e0a8e 419
hkjung 1:bdb1c9f07d06 420 if( !done ) {
hkjung 1:bdb1c9f07d06 421 done = (_parser->recv("+CMGS: %d", &msg_idx) && _parser->recv("OK"));
hkjung 1:bdb1c9f07d06 422 if(done) {
hkjung 1:bdb1c9f07d06 423 devlog(">> SMS send success: index %d\r\n", msg_idx);
hkjung 1:bdb1c9f07d06 424 ret = RET_OK;
hkjung 1:bdb1c9f07d06 425 }
hkjung 0:5cd6077e0a8e 426 }
hkjung 0:5cd6077e0a8e 427 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 428
hkjung 0:5cd6077e0a8e 429 return ret;
hkjung 0:5cd6077e0a8e 430 }
hkjung 0:5cd6077e0a8e 431
hkjung 1:bdb1c9f07d06 432 int checkRecvSMS_BG96(void)
hkjung 1:bdb1c9f07d06 433 {
hkjung 1:bdb1c9f07d06 434 int ret = RET_NOK;
hkjung 1:bdb1c9f07d06 435 int msg_idx = 0;
hkjung 0:5cd6077e0a8e 436
hkjung 1:bdb1c9f07d06 437 bool received = false;
hkjung 0:5cd6077e0a8e 438
hkjung 1:bdb1c9f07d06 439 _parser->set_timeout(1);
hkjung 1:bdb1c9f07d06 440 received = _parser->recv("+CMTI: \"ME\",%d", &msg_idx);
hkjung 0:5cd6077e0a8e 441 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 442
hkjung 1:bdb1c9f07d06 443 if(received) {
hkjung 1:bdb1c9f07d06 444 ret = msg_idx;
hkjung 1:bdb1c9f07d06 445 devlog("<< SMS received: index %d\r\n", msg_idx);
hkjung 1:bdb1c9f07d06 446 }
hkjung 0:5cd6077e0a8e 447 return ret;
hkjung 0:5cd6077e0a8e 448 }
hkjung 0:5cd6077e0a8e 449
hkjung 1:bdb1c9f07d06 450 int8_t recvSMS_BG96(int msg_idx, char * msg, char * da, char * datetime)
hkjung 0:5cd6077e0a8e 451 {
hkjung 1:bdb1c9f07d06 452 int8_t ret = RET_NOK;
hkjung 1:bdb1c9f07d06 453 char type[15] = {0, };
hkjung 1:bdb1c9f07d06 454
hkjung 1:bdb1c9f07d06 455 int i = 0;
hkjung 1:bdb1c9f07d06 456 bool done = false;
hkjung 1:bdb1c9f07d06 457 char recv_msg[MAX_SMS_SIZE] = {0, };
hkjung 1:bdb1c9f07d06 458 char * search_pt;
hkjung 1:bdb1c9f07d06 459 Timer t;
hkjung 1:bdb1c9f07d06 460
hkjung 1:bdb1c9f07d06 461 memset(recv_msg, 0x00, MAX_SMS_SIZE);
hkjung 1:bdb1c9f07d06 462
hkjung 1:bdb1c9f07d06 463 _parser->set_timeout(BG96_RECV_TIMEOUT);
hkjung 0:5cd6077e0a8e 464
hkjung 1:bdb1c9f07d06 465 if( _parser->send("AT+CMGR=%d", msg_idx)
hkjung 1:bdb1c9f07d06 466 && _parser->recv("+CMGR: \"%[^\"]\",\"%[^\"]\",,\"%[^\"]\"", type, da, datetime))
hkjung 1:bdb1c9f07d06 467 {
hkjung 1:bdb1c9f07d06 468 // timer start
hkjung 1:bdb1c9f07d06 469 t.start();
hkjung 1:bdb1c9f07d06 470
hkjung 1:bdb1c9f07d06 471 while( !done && (t.read_ms() < BG96_DEFAULT_TIMEOUT)) {
hkjung 1:bdb1c9f07d06 472 _parser->read(&recv_msg[i++], 1);
hkjung 1:bdb1c9f07d06 473 search_pt = strstr(recv_msg, "OK");
hkjung 1:bdb1c9f07d06 474 if (search_pt != 0) {
hkjung 1:bdb1c9f07d06 475 done = true; // break;
hkjung 1:bdb1c9f07d06 476 }
hkjung 1:bdb1c9f07d06 477 }
hkjung 1:bdb1c9f07d06 478 if(i > 8) {
hkjung 1:bdb1c9f07d06 479 memcpy(msg, recv_msg+2, i-8);
hkjung 1:bdb1c9f07d06 480 devlog("<< SMS receive success: index %d\r\n", msg_idx);
hkjung 1:bdb1c9f07d06 481 ret = RET_OK;
hkjung 1:bdb1c9f07d06 482 }
hkjung 1:bdb1c9f07d06 483 }
hkjung 1:bdb1c9f07d06 484 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 1:bdb1c9f07d06 485 _parser->flush();
hkjung 1:bdb1c9f07d06 486
hkjung 0:5cd6077e0a8e 487 return ret;
hkjung 0:5cd6077e0a8e 488 }
hkjung 0:5cd6077e0a8e 489
hkjung 1:bdb1c9f07d06 490 int8_t deleteSMS_BG96(int msg_idx)
hkjung 0:5cd6077e0a8e 491 {
hkjung 0:5cd6077e0a8e 492 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 493
hkjung 1:bdb1c9f07d06 494 if(_parser->send("AT+CMGD=%d", msg_idx) && _parser->recv("OK"))
hkjung 1:bdb1c9f07d06 495 {
hkjung 1:bdb1c9f07d06 496 devlog("Message index[%d] has been deleted\r\n", msg_idx);
hkjung 1:bdb1c9f07d06 497 ret = RET_OK;
hkjung 1:bdb1c9f07d06 498 }
hkjung 0:5cd6077e0a8e 499 return ret;
hkjung 0:5cd6077e0a8e 500 }
hkjung 1:bdb1c9f07d06 501
hkjung 1:bdb1c9f07d06 502 int8_t deleteAllSMS_BG96(int delflag)
hkjung 1:bdb1c9f07d06 503 {
hkjung 1:bdb1c9f07d06 504 int8_t ret = RET_NOK;
hkjung 1:bdb1c9f07d06 505
hkjung 1:bdb1c9f07d06 506 //delflag == 1; // Delete all read messages from storage
hkjung 1:bdb1c9f07d06 507 //delflag == 2; // Delete all read messages from storage and sent mobile originated messages
hkjung 1:bdb1c9f07d06 508 //delflag == 3; // Delete all read messages from storage, sent and unsent mobile originated messages
hkjung 1:bdb1c9f07d06 509 //delflag == 4; // Delete all messages from storage
hkjung 1:bdb1c9f07d06 510
hkjung 1:bdb1c9f07d06 511 if(_parser->send("AT+CMGD=0,%d", delflag) && _parser->recv("OK"))
hkjung 1:bdb1c9f07d06 512 {
hkjung 1:bdb1c9f07d06 513 devlog("All messages has been deleted (delflag: %d)\r\n", delflag);
hkjung 1:bdb1c9f07d06 514 ret = RET_OK;
hkjung 1:bdb1c9f07d06 515 }
hkjung 1:bdb1c9f07d06 516 return ret;
hkjung 1:bdb1c9f07d06 517 }
hkjung 1:bdb1c9f07d06 518
hkjung 1:bdb1c9f07d06 519