WIZnet-IoTShield-AMM592-SMS example

Committer:
hkjung
Date:
Tue Mar 19 00:25:29 2019 +0000
Revision:
0:5cd6077e0a8e
Child:
1:bdb1c9f07d06
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 0:5cd6077e0a8e 58 #define MBED_CONF_IOTSHIELD_CATM1_RESET D6
hkjung 0:5cd6077e0a8e 59 #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9
hkjung 0:5cd6077e0a8e 60 #define MBED_CONF_IOTSHIELD_CATM1_WAKE NC
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 0:5cd6077e0a8e 69 // Functions: Module Status
hkjung 0:5cd6077e0a8e 70 void waitCatM1Ready(void);
hkjung 0:5cd6077e0a8e 71 int8_t setEchoStatus_BG96(bool onoff);
hkjung 0:5cd6077e0a8e 72 int8_t getUsimStatus_BG96(void);
hkjung 0:5cd6077e0a8e 73 int8_t getNetworkStatus_BG96(void);
hkjung 0:5cd6077e0a8e 74 int8_t checknSetApn_BG96(const char * apn);
hkjung 0:5cd6077e0a8e 75 int8_t getFirmwareVersion_BG96(char * version);
hkjung 0:5cd6077e0a8e 76
hkjung 0:5cd6077e0a8e 77 // Functions: PDP context
hkjung 0:5cd6077e0a8e 78 int8_t setContextActivate_BG96(void); // Activate a PDP Context
hkjung 0:5cd6077e0a8e 79 int8_t setContextDeactivate_BG96(void); // Deactivate a PDP Context
hkjung 0:5cd6077e0a8e 80
hkjung 0:5cd6077e0a8e 81 // Functions: Ping test
hkjung 0:5cd6077e0a8e 82 void pingToHost_BG96(char * host, int timeout, int pingnum);
hkjung 0:5cd6077e0a8e 83
hkjung 0:5cd6077e0a8e 84
hkjung 0:5cd6077e0a8e 85 Serial pc(USBTX, USBRX); // USB debug
hkjung 0:5cd6077e0a8e 86
hkjung 0:5cd6077e0a8e 87 UARTSerial *_serial; // Cat.M1 module
hkjung 0:5cd6077e0a8e 88 ATCmdParser *_parser;
hkjung 0:5cd6077e0a8e 89
hkjung 0:5cd6077e0a8e 90
hkjung 0:5cd6077e0a8e 91 void serialPcInit(void)
hkjung 0:5cd6077e0a8e 92 {
hkjung 0:5cd6077e0a8e 93 pc.baud(115200);
hkjung 0:5cd6077e0a8e 94 pc.format(8, Serial::None, 1);
hkjung 0:5cd6077e0a8e 95 }
hkjung 0:5cd6077e0a8e 96
hkjung 0:5cd6077e0a8e 97 void serialDeviceInit(PinName tx, PinName rx, int baudrate)
hkjung 0:5cd6077e0a8e 98 {
hkjung 0:5cd6077e0a8e 99 _serial = new UARTSerial(tx, rx, baudrate);
hkjung 0:5cd6077e0a8e 100 }
hkjung 0:5cd6077e0a8e 101
hkjung 0:5cd6077e0a8e 102 void serialAtParserInit(const char *delimiter, bool debug_en)
hkjung 0:5cd6077e0a8e 103 {
hkjung 0:5cd6077e0a8e 104 _parser = new ATCmdParser(_serial);
hkjung 0:5cd6077e0a8e 105 _parser->debug_on(debug_en);
hkjung 0:5cd6077e0a8e 106 _parser->set_delimiter(delimiter);
hkjung 0:5cd6077e0a8e 107 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 108 }
hkjung 0:5cd6077e0a8e 109
hkjung 0:5cd6077e0a8e 110 void catm1DeviceInit(void)
hkjung 0:5cd6077e0a8e 111 {
hkjung 0:5cd6077e0a8e 112 serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX,
hkjung 0:5cd6077e0a8e 113 MBED_CONF_IOTSHIELD_CATM1_RX,
hkjung 0:5cd6077e0a8e 114 BG96_DEFAULT_BAUD_RATE);
hkjung 0:5cd6077e0a8e 115
hkjung 0:5cd6077e0a8e 116 serialAtParserInit( BG96_PARSER_DELIMITER,
hkjung 0:5cd6077e0a8e 117 BG96_PARSER_DEBUG);
hkjung 0:5cd6077e0a8e 118 }
hkjung 0:5cd6077e0a8e 119
hkjung 0:5cd6077e0a8e 120 void printPingToHost_BG96(char * host, int timeout, int pingnum)
hkjung 0:5cd6077e0a8e 121 {
hkjung 0:5cd6077e0a8e 122 int i;
hkjung 0:5cd6077e0a8e 123 char resp_str[50] = {0, };
hkjung 0:5cd6077e0a8e 124
hkjung 0:5cd6077e0a8e 125 if((timeout < 1) || (timeout > 255)) {
hkjung 0:5cd6077e0a8e 126 devlog("Ping timeout range is 1-255, and the default value is 4 (unit: sec)\r\n");
hkjung 0:5cd6077e0a8e 127 return;
hkjung 0:5cd6077e0a8e 128 }
hkjung 0:5cd6077e0a8e 129
hkjung 0:5cd6077e0a8e 130 if((pingnum < 1) || (pingnum > 10)) {
hkjung 0:5cd6077e0a8e 131 devlog("The maximum number of sending Ping request range is 1-10, and the default value is 4\r\n");
hkjung 0:5cd6077e0a8e 132 return;
hkjung 0:5cd6077e0a8e 133 }
hkjung 0:5cd6077e0a8e 134
hkjung 0:5cd6077e0a8e 135 _parser->set_timeout((1000 * timeout) + 2000);
hkjung 0:5cd6077e0a8e 136
hkjung 0:5cd6077e0a8e 137 if(_parser->send("AT+QPING=%d,\"%s\",%d,%d", 1, host, timeout, pingnum) && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 138 for(i = 0; i < (pingnum); i++) {
hkjung 0:5cd6077e0a8e 139 _parser->recv("+QPING: %s\n", resp_str);
hkjung 0:5cd6077e0a8e 140 devlog("%s: %s\r\n", host, resp_str);
hkjung 0:5cd6077e0a8e 141 }
hkjung 0:5cd6077e0a8e 142
hkjung 0:5cd6077e0a8e 143 }
hkjung 0:5cd6077e0a8e 144
hkjung 0:5cd6077e0a8e 145 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 146 _parser->flush();
hkjung 0:5cd6077e0a8e 147 }
hkjung 0:5cd6077e0a8e 148
hkjung 0:5cd6077e0a8e 149 int main()
hkjung 0:5cd6077e0a8e 150 {
hkjung 0:5cd6077e0a8e 151 serialPcInit();
hkjung 0:5cd6077e0a8e 152 catm1DeviceInit();
hkjung 0:5cd6077e0a8e 153
hkjung 0:5cd6077e0a8e 154 myprintf("Waiting for Cat.M1 Module Ready...\r\n");
hkjung 0:5cd6077e0a8e 155
hkjung 0:5cd6077e0a8e 156 // Todo: BG96 Hardware Init (hardware reset & pwrkey act)
hkjung 0:5cd6077e0a8e 157
hkjung 0:5cd6077e0a8e 158 waitCatM1Ready();
hkjung 0:5cd6077e0a8e 159
hkjung 0:5cd6077e0a8e 160 myprintf("System Init Complete\r\n");
hkjung 0:5cd6077e0a8e 161
hkjung 0:5cd6077e0a8e 162 myprintf("WIZnet IoT Shield for Arm MBED");
hkjung 0:5cd6077e0a8e 163 myprintf("LTE Cat.M1 Version");
hkjung 0:5cd6077e0a8e 164 myprintf("=================================================");
hkjung 0:5cd6077e0a8e 165 myprintf(">> Target Board: WIoT-QC01 (Quectel BG96)");
hkjung 0:5cd6077e0a8e 166 myprintf(">> Sample Code: Ping Test");
hkjung 0:5cd6077e0a8e 167 myprintf("=================================================\r\n");
hkjung 0:5cd6077e0a8e 168
hkjung 0:5cd6077e0a8e 169 setEchoStatus_BG96(OFF);
hkjung 0:5cd6077e0a8e 170
hkjung 0:5cd6077e0a8e 171 getUsimStatus_BG96();
hkjung 0:5cd6077e0a8e 172
hkjung 0:5cd6077e0a8e 173 getNetworkStatus_BG96();
hkjung 0:5cd6077e0a8e 174
hkjung 0:5cd6077e0a8e 175 checknSetApn_BG96(CATM1_APN_SKT);
hkjung 0:5cd6077e0a8e 176
hkjung 0:5cd6077e0a8e 177 char ping_dest_1st[] = "8.8.8.8";
hkjung 0:5cd6077e0a8e 178 char ping_dest_2nd[] = "www.google.com";
hkjung 0:5cd6077e0a8e 179
hkjung 0:5cd6077e0a8e 180 setContextActivate_BG96();
hkjung 0:5cd6077e0a8e 181
hkjung 0:5cd6077e0a8e 182 myprintf("[Ping] Host: %s\r\n", ping_dest_1st);
hkjung 0:5cd6077e0a8e 183 printPingToHost_BG96(ping_dest_1st, 2, 4);
hkjung 0:5cd6077e0a8e 184
hkjung 0:5cd6077e0a8e 185 wait_ms(2000);
hkjung 0:5cd6077e0a8e 186
hkjung 0:5cd6077e0a8e 187 myprintf("[Ping] Host: %s\r\n", ping_dest_2nd);
hkjung 0:5cd6077e0a8e 188 printPingToHost_BG96(ping_dest_2nd, 2, 4);
hkjung 0:5cd6077e0a8e 189
hkjung 0:5cd6077e0a8e 190 setContextDeactivate_BG96();
hkjung 0:5cd6077e0a8e 191 }
hkjung 0:5cd6077e0a8e 192
hkjung 0:5cd6077e0a8e 193 void waitCatM1Ready(void)
hkjung 0:5cd6077e0a8e 194 {
hkjung 0:5cd6077e0a8e 195 while(1)
hkjung 0:5cd6077e0a8e 196 {
hkjung 0:5cd6077e0a8e 197 if(_parser->recv("RDY"))
hkjung 0:5cd6077e0a8e 198 {
hkjung 0:5cd6077e0a8e 199 myprintf("BG96 ready\r\n");
hkjung 0:5cd6077e0a8e 200 return ;
hkjung 0:5cd6077e0a8e 201 }
hkjung 0:5cd6077e0a8e 202 else if(_parser->send("AT") && _parser->recv("OK"))
hkjung 0:5cd6077e0a8e 203 {
hkjung 0:5cd6077e0a8e 204 myprintf("BG96 already available\r\n");
hkjung 0:5cd6077e0a8e 205 return ;
hkjung 0:5cd6077e0a8e 206 }
hkjung 0:5cd6077e0a8e 207 }
hkjung 0:5cd6077e0a8e 208 }
hkjung 0:5cd6077e0a8e 209
hkjung 0:5cd6077e0a8e 210 int8_t setEchoStatus_BG96(bool onoff)
hkjung 0:5cd6077e0a8e 211 {
hkjung 0:5cd6077e0a8e 212 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 213 char _buf[10];
hkjung 0:5cd6077e0a8e 214
hkjung 0:5cd6077e0a8e 215 sprintf((char *)_buf, "ATE%d", onoff);
hkjung 0:5cd6077e0a8e 216
hkjung 0:5cd6077e0a8e 217 if(_parser->send(_buf) && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 218 devlog("Turn Echo %s success\r\n", onoff?"ON":"OFF");
hkjung 0:5cd6077e0a8e 219 ret = RET_OK;
hkjung 0:5cd6077e0a8e 220 } else {
hkjung 0:5cd6077e0a8e 221 devlog("Turn Echo %s failed\r\n", onoff?"ON":"OFF");
hkjung 0:5cd6077e0a8e 222 }
hkjung 0:5cd6077e0a8e 223 return ret;
hkjung 0:5cd6077e0a8e 224 }
hkjung 0:5cd6077e0a8e 225
hkjung 0:5cd6077e0a8e 226 int8_t getUsimStatus_BG96(void)
hkjung 0:5cd6077e0a8e 227 {
hkjung 0:5cd6077e0a8e 228 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 229
hkjung 0:5cd6077e0a8e 230 _parser->send("AT+CPIN?");
hkjung 0:5cd6077e0a8e 231 if(_parser->recv("+CPIN: READY") && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 232 devlog("USIM Status: READY\r\n");
hkjung 0:5cd6077e0a8e 233 ret = RET_OK;
hkjung 0:5cd6077e0a8e 234 } else {
hkjung 0:5cd6077e0a8e 235 devlog("Retrieving USIM Status failed\r\n");
hkjung 0:5cd6077e0a8e 236 }
hkjung 0:5cd6077e0a8e 237 return ret;
hkjung 0:5cd6077e0a8e 238 }
hkjung 0:5cd6077e0a8e 239
hkjung 0:5cd6077e0a8e 240 int8_t getNetworkStatus_BG96(void)
hkjung 0:5cd6077e0a8e 241 {
hkjung 0:5cd6077e0a8e 242 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 243
hkjung 0:5cd6077e0a8e 244 if(_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"SRV\"") && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 245 devlog("Network Status: attached\r\n");
hkjung 0:5cd6077e0a8e 246 ret = RET_OK;
hkjung 0:5cd6077e0a8e 247 } else if (_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"LIMITED\"") && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 248 devlog("Network Status: limited\r\n");
hkjung 0:5cd6077e0a8e 249 ret = RET_OK;
hkjung 0:5cd6077e0a8e 250 } else {
hkjung 0:5cd6077e0a8e 251 devlog("Network Status: Error\r\n");
hkjung 0:5cd6077e0a8e 252 }
hkjung 0:5cd6077e0a8e 253 return ret;
hkjung 0:5cd6077e0a8e 254 }
hkjung 0:5cd6077e0a8e 255
hkjung 0:5cd6077e0a8e 256 int8_t checknSetApn_BG96(const char * apn) // Configure Parameters of a TCP/IP Context
hkjung 0:5cd6077e0a8e 257 {
hkjung 0:5cd6077e0a8e 258 char resp_str[100];
hkjung 0:5cd6077e0a8e 259
hkjung 0:5cd6077e0a8e 260 uint16_t i = 0;
hkjung 0:5cd6077e0a8e 261 char * search_pt;
hkjung 0:5cd6077e0a8e 262
hkjung 0:5cd6077e0a8e 263 memset(resp_str, 0, sizeof(resp_str));
hkjung 0:5cd6077e0a8e 264
hkjung 0:5cd6077e0a8e 265 devlog("Checking APN...\r\n");
hkjung 0:5cd6077e0a8e 266
hkjung 0:5cd6077e0a8e 267 _parser->send("AT+QICSGP=1");
hkjung 0:5cd6077e0a8e 268
hkjung 0:5cd6077e0a8e 269 while(1)
hkjung 0:5cd6077e0a8e 270 {
hkjung 0:5cd6077e0a8e 271 _parser->read(&resp_str[i++], 1);
hkjung 0:5cd6077e0a8e 272 search_pt = strstr(resp_str, "OK\r\n");
hkjung 0:5cd6077e0a8e 273 if (search_pt != 0)
hkjung 0:5cd6077e0a8e 274 {
hkjung 0:5cd6077e0a8e 275 break;
hkjung 0:5cd6077e0a8e 276 }
hkjung 0:5cd6077e0a8e 277 }
hkjung 0:5cd6077e0a8e 278
hkjung 0:5cd6077e0a8e 279 search_pt = strstr(resp_str, apn);
hkjung 0:5cd6077e0a8e 280 if (search_pt == 0)
hkjung 0:5cd6077e0a8e 281 {
hkjung 0:5cd6077e0a8e 282 devlog("Mismatched APN: %s\r\n", resp_str);
hkjung 0:5cd6077e0a8e 283 devlog("Storing APN %s...\r\n", apn);
hkjung 0:5cd6077e0a8e 284 if(!(_parser->send("AT+QICSGP=1,%d,\"%s\",\"\",\"\",0", BG96_APN_PROTOCOL, apn) && _parser->recv("OK")))
hkjung 0:5cd6077e0a8e 285 {
hkjung 0:5cd6077e0a8e 286 return RET_NOK; // failed
hkjung 0:5cd6077e0a8e 287 }
hkjung 0:5cd6077e0a8e 288 }
hkjung 0:5cd6077e0a8e 289 devlog("APN Check Done\r\n");
hkjung 0:5cd6077e0a8e 290
hkjung 0:5cd6077e0a8e 291 return RET_OK;
hkjung 0:5cd6077e0a8e 292 }
hkjung 0:5cd6077e0a8e 293
hkjung 0:5cd6077e0a8e 294 int8_t getFirmwareVersion_BG96(char * version)
hkjung 0:5cd6077e0a8e 295 {
hkjung 0:5cd6077e0a8e 296 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 297
hkjung 0:5cd6077e0a8e 298 if(_parser->send("AT+QGMR") && _parser->recv("%s\n", version) && _parser->recv("OK"))
hkjung 0:5cd6077e0a8e 299 {
hkjung 0:5cd6077e0a8e 300 ret = RET_OK;
hkjung 0:5cd6077e0a8e 301 }
hkjung 0:5cd6077e0a8e 302 return ret;
hkjung 0:5cd6077e0a8e 303 }
hkjung 0:5cd6077e0a8e 304
hkjung 0:5cd6077e0a8e 305 int8_t getImeiNumber_BG96(char * imei)
hkjung 0:5cd6077e0a8e 306 {
hkjung 0:5cd6077e0a8e 307 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 308
hkjung 0:5cd6077e0a8e 309 if(_parser->send("AT+CGSN") && _parser->recv("%s\n", imei) && _parser->recv("OK"))
hkjung 0:5cd6077e0a8e 310 {
hkjung 0:5cd6077e0a8e 311 ret = RET_OK;
hkjung 0:5cd6077e0a8e 312 }
hkjung 0:5cd6077e0a8e 313 return ret;
hkjung 0:5cd6077e0a8e 314 }
hkjung 0:5cd6077e0a8e 315
hkjung 0:5cd6077e0a8e 316 int8_t getIpAddressByName_BG96(const char * name, char * ipstr)
hkjung 0:5cd6077e0a8e 317 {
hkjung 0:5cd6077e0a8e 318 char buf2[50];
hkjung 0:5cd6077e0a8e 319 bool ok;
hkjung 0:5cd6077e0a8e 320 int err, ipcount, dnsttl;
hkjung 0:5cd6077e0a8e 321
hkjung 0:5cd6077e0a8e 322 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 323
hkjung 0:5cd6077e0a8e 324 ok = ( _parser->send("AT+QIDNSGIP=1,\"%s\"", name)
hkjung 0:5cd6077e0a8e 325 && _parser->recv("OK")
hkjung 0:5cd6077e0a8e 326 && _parser->recv("+QIURC: \"dnsgip\",%d,%d,%d", &err, &ipcount, &dnsttl)
hkjung 0:5cd6077e0a8e 327 && err==0
hkjung 0:5cd6077e0a8e 328 && ipcount > 0
hkjung 0:5cd6077e0a8e 329 );
hkjung 0:5cd6077e0a8e 330
hkjung 0:5cd6077e0a8e 331 if( ok ) {
hkjung 0:5cd6077e0a8e 332 _parser->recv("+QIURC: \"dnsgip\",\"%[^\"]\"", ipstr); //use the first DNS value
hkjung 0:5cd6077e0a8e 333 for( int i=0; i<ipcount-1; i++ )
hkjung 0:5cd6077e0a8e 334 _parser->recv("+QIURC: \"dnsgip\",\"%[^\"]\"", buf2); //and discrard the rest if >1
hkjung 0:5cd6077e0a8e 335
hkjung 0:5cd6077e0a8e 336 ret = RET_OK;
hkjung 0:5cd6077e0a8e 337 }
hkjung 0:5cd6077e0a8e 338 return ret;
hkjung 0:5cd6077e0a8e 339 }
hkjung 0:5cd6077e0a8e 340
hkjung 0:5cd6077e0a8e 341 int8_t getIpAddress_BG96(char * ipstr) // IPv4 or IPv6
hkjung 0:5cd6077e0a8e 342 {
hkjung 0:5cd6077e0a8e 343 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 344 int id, state, type; // not used
hkjung 0:5cd6077e0a8e 345
hkjung 0:5cd6077e0a8e 346 _parser->send("AT+QIACT?");
hkjung 0:5cd6077e0a8e 347 if(_parser->recv("+QIACT: %d,%d,%d,\"%[^\"]\"", &id, &state, &type, ipstr)
hkjung 0:5cd6077e0a8e 348 && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 349 ret = RET_OK;
hkjung 0:5cd6077e0a8e 350 }
hkjung 0:5cd6077e0a8e 351 return ret;
hkjung 0:5cd6077e0a8e 352 }
hkjung 0:5cd6077e0a8e 353
hkjung 0:5cd6077e0a8e 354 int8_t setContextActivate_BG96(void) // Activate a PDP Context
hkjung 0:5cd6077e0a8e 355 {
hkjung 0:5cd6077e0a8e 356 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 357
hkjung 0:5cd6077e0a8e 358 _parser->send("AT+QIACT=1");
hkjung 0:5cd6077e0a8e 359 if(_parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 360 devlog("Activate a PDP Context\r\n");
hkjung 0:5cd6077e0a8e 361 ret = RET_OK;
hkjung 0:5cd6077e0a8e 362 } else {
hkjung 0:5cd6077e0a8e 363 devlog("PDP Context Activation failed\r\n");
hkjung 0:5cd6077e0a8e 364 }
hkjung 0:5cd6077e0a8e 365 return ret;
hkjung 0:5cd6077e0a8e 366 }
hkjung 0:5cd6077e0a8e 367
hkjung 0:5cd6077e0a8e 368 int8_t setContextDeactivate_BG96(void) // Deactivate a PDP Context
hkjung 0:5cd6077e0a8e 369 {
hkjung 0:5cd6077e0a8e 370 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 371
hkjung 0:5cd6077e0a8e 372 _parser->send("AT+QIDEACT=1");
hkjung 0:5cd6077e0a8e 373 if(_parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 374 devlog("Deactivate a PDP Context\r\n");
hkjung 0:5cd6077e0a8e 375 ret = RET_OK;
hkjung 0:5cd6077e0a8e 376 } else {
hkjung 0:5cd6077e0a8e 377 devlog("PDP Context Deactivation failed\r\n");
hkjung 0:5cd6077e0a8e 378 }
hkjung 0:5cd6077e0a8e 379 return ret;
hkjung 0:5cd6077e0a8e 380 }
hkjung 0:5cd6077e0a8e 381
hkjung 0:5cd6077e0a8e 382 int8_t sockOpenConnect_BG96(const char * type, const char * addr, int port)
hkjung 0:5cd6077e0a8e 383 {
hkjung 0:5cd6077e0a8e 384 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 385 int err = 1;
hkjung 0:5cd6077e0a8e 386 int id = 0;
hkjung 0:5cd6077e0a8e 387
hkjung 0:5cd6077e0a8e 388 bool done = false;
hkjung 0:5cd6077e0a8e 389 Timer t;
hkjung 0:5cd6077e0a8e 390
hkjung 0:5cd6077e0a8e 391 _parser->set_timeout(BG96_CONNECT_TIMEOUT);
hkjung 0:5cd6077e0a8e 392
hkjung 0:5cd6077e0a8e 393 if((strcmp(type, "TCP") != 0) && (strcmp(type, "UDP") != 0)) {
hkjung 0:5cd6077e0a8e 394 return RET_NOK;
hkjung 0:5cd6077e0a8e 395 }
hkjung 0:5cd6077e0a8e 396
hkjung 0:5cd6077e0a8e 397 t.start();
hkjung 0:5cd6077e0a8e 398
hkjung 0:5cd6077e0a8e 399 _parser->send("AT+QIOPEN=1,%d,\"%s\",\"%s\",%d", id, type, addr, port);
hkjung 0:5cd6077e0a8e 400 do {
hkjung 0:5cd6077e0a8e 401 done = (_parser->recv("+QIOPEN: %d,%d", &id, &err) && (err == 0));
hkjung 0:5cd6077e0a8e 402 } while(!done && t.read_ms() < BG96_CONNECT_TIMEOUT);
hkjung 0:5cd6077e0a8e 403
hkjung 0:5cd6077e0a8e 404 if(done) ret = RET_OK;
hkjung 0:5cd6077e0a8e 405
hkjung 0:5cd6077e0a8e 406 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 407 _parser->flush();
hkjung 0:5cd6077e0a8e 408
hkjung 0:5cd6077e0a8e 409 return ret;
hkjung 0:5cd6077e0a8e 410 }
hkjung 0:5cd6077e0a8e 411
hkjung 0:5cd6077e0a8e 412 int8_t sockClose_BG96(void)
hkjung 0:5cd6077e0a8e 413 {
hkjung 0:5cd6077e0a8e 414 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 415 int id = 0;
hkjung 0:5cd6077e0a8e 416
hkjung 0:5cd6077e0a8e 417 _parser->set_timeout(BG96_CONNECT_TIMEOUT);
hkjung 0:5cd6077e0a8e 418
hkjung 0:5cd6077e0a8e 419 if(_parser->send("AT+QICLOSE=%d", id) && _parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 420 ret = RET_OK;
hkjung 0:5cd6077e0a8e 421 }
hkjung 0:5cd6077e0a8e 422 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 423
hkjung 0:5cd6077e0a8e 424 return ret;
hkjung 0:5cd6077e0a8e 425 }
hkjung 0:5cd6077e0a8e 426
hkjung 0:5cd6077e0a8e 427 int8_t dataSend_BG96(char * data, int len)
hkjung 0:5cd6077e0a8e 428 {
hkjung 0:5cd6077e0a8e 429 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 430 int id = 0;
hkjung 0:5cd6077e0a8e 431 bool done = false;
hkjung 0:5cd6077e0a8e 432
hkjung 0:5cd6077e0a8e 433 _parser->set_timeout(BG96_SEND_TIMEOUT);
hkjung 0:5cd6077e0a8e 434
hkjung 0:5cd6077e0a8e 435 _parser->send("AT+QISEND=%d,%ld", id, len);
hkjung 0:5cd6077e0a8e 436 if( !done && _parser->recv(">") )
hkjung 0:5cd6077e0a8e 437 done = (_parser->write(data, len) <= 0);
hkjung 0:5cd6077e0a8e 438
hkjung 0:5cd6077e0a8e 439 if( !done )
hkjung 0:5cd6077e0a8e 440 done = _parser->recv("SEND OK");
hkjung 0:5cd6077e0a8e 441
hkjung 0:5cd6077e0a8e 442 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 443
hkjung 0:5cd6077e0a8e 444 return ret;
hkjung 0:5cd6077e0a8e 445 }
hkjung 0:5cd6077e0a8e 446
hkjung 0:5cd6077e0a8e 447 int8_t checkDataRecv_BG96(void)
hkjung 0:5cd6077e0a8e 448 {
hkjung 0:5cd6077e0a8e 449 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 450 int id = 0;
hkjung 0:5cd6077e0a8e 451 char cmd[20];
hkjung 0:5cd6077e0a8e 452
hkjung 0:5cd6077e0a8e 453 bool received = false;
hkjung 0:5cd6077e0a8e 454
hkjung 0:5cd6077e0a8e 455 sprintf(cmd, "+QIURC: \"recv\",%d", id);
hkjung 0:5cd6077e0a8e 456 _parser->set_timeout(1);
hkjung 0:5cd6077e0a8e 457 received = _parser->recv(cmd);
hkjung 0:5cd6077e0a8e 458 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 459
hkjung 0:5cd6077e0a8e 460 if(received) ret = RET_OK;
hkjung 0:5cd6077e0a8e 461 return ret;
hkjung 0:5cd6077e0a8e 462 }
hkjung 0:5cd6077e0a8e 463
hkjung 0:5cd6077e0a8e 464 int8_t dataRecv_BG96(char * data, int * len)
hkjung 0:5cd6077e0a8e 465 {
hkjung 0:5cd6077e0a8e 466 int8_t ret = RET_NOK;
hkjung 0:5cd6077e0a8e 467 int id = 0;
hkjung 0:5cd6077e0a8e 468 int recvCount = 0;
hkjung 0:5cd6077e0a8e 469
hkjung 0:5cd6077e0a8e 470 _parser->set_timeout(BG96_RECV_TIMEOUT);
hkjung 0:5cd6077e0a8e 471
hkjung 0:5cd6077e0a8e 472 if( _parser->send("AT+QIRD=%d", id) && _parser->recv("+QIRD:%d\r\n",&recvCount) ) {
hkjung 0:5cd6077e0a8e 473 if(recvCount > 0) {
hkjung 0:5cd6077e0a8e 474 _parser->getc();
hkjung 0:5cd6077e0a8e 475 _parser->read(data, recvCount);
hkjung 0:5cd6077e0a8e 476 if(_parser->recv("OK")) {
hkjung 0:5cd6077e0a8e 477 ret = RET_OK;
hkjung 0:5cd6077e0a8e 478 } else {
hkjung 0:5cd6077e0a8e 479 recvCount = 0;
hkjung 0:5cd6077e0a8e 480 }
hkjung 0:5cd6077e0a8e 481 }
hkjung 0:5cd6077e0a8e 482 }
hkjung 0:5cd6077e0a8e 483 _parser->set_timeout(BG96_DEFAULT_TIMEOUT);
hkjung 0:5cd6077e0a8e 484 _parser->flush();
hkjung 0:5cd6077e0a8e 485
hkjung 0:5cd6077e0a8e 486 *len = recvCount;
hkjung 0:5cd6077e0a8e 487
hkjung 0:5cd6077e0a8e 488 return ret;
hkjung 0:5cd6077e0a8e 489 }