Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:2e1002e6798a, 2019-09-18 (annotated)
- Committer:
- AustinKim
- Date:
- Wed Sep 18 07:12:53 2019 +0000
- Revision:
- 2:2e1002e6798a
- Parent:
- 1:1c65b1dd9e7a
Update main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AustinKim | 0:8f61900631fe | 1 | /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED |
AustinKim | 0:8f61900631fe | 2 | * Copyright (c) 2019 WIZnet Co., Ltd. |
AustinKim | 0:8f61900631fe | 3 | * SPDX-License-Identifier: Apache-2.0 |
AustinKim | 0:8f61900631fe | 4 | */ |
AustinKim | 0:8f61900631fe | 5 | |
AustinKim | 0:8f61900631fe | 6 | /* |
AustinKim | 0:8f61900631fe | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AustinKim | 0:8f61900631fe | 8 | * you may not use this file except in compliance with the License. |
AustinKim | 0:8f61900631fe | 9 | * You may obtain a copy of the License at |
AustinKim | 0:8f61900631fe | 10 | * |
AustinKim | 0:8f61900631fe | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
AustinKim | 0:8f61900631fe | 12 | * |
AustinKim | 0:8f61900631fe | 13 | * Unless required by applicable law or agreed to in writing, software |
AustinKim | 0:8f61900631fe | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
AustinKim | 0:8f61900631fe | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AustinKim | 0:8f61900631fe | 16 | * |
AustinKim | 0:8f61900631fe | 17 | * See the License for the specific language governing permissions and |
AustinKim | 0:8f61900631fe | 18 | * limitations under the License. |
AustinKim | 0:8f61900631fe | 19 | * |
AustinKim | 0:8f61900631fe | 20 | */ |
AustinKim | 0:8f61900631fe | 21 | |
AustinKim | 0:8f61900631fe | 22 | #include "mbed.h" |
AustinKim | 0:8f61900631fe | 23 | |
AustinKim | 0:8f61900631fe | 24 | #include <string> |
AustinKim | 0:8f61900631fe | 25 | |
AustinKim | 0:8f61900631fe | 26 | #define RET_OK 1 |
AustinKim | 0:8f61900631fe | 27 | #define RET_NOK -1 |
AustinKim | 0:8f61900631fe | 28 | #define DEBUG_ENABLE 1 |
AustinKim | 0:8f61900631fe | 29 | #define DEBUG_DISABLE 0 |
AustinKim | 0:8f61900631fe | 30 | #define ON 1 |
AustinKim | 0:8f61900631fe | 31 | #define OFF 0 |
AustinKim | 0:8f61900631fe | 32 | |
AustinKim | 0:8f61900631fe | 33 | #define MAX_BUF_SIZE 1024 |
AustinKim | 0:8f61900631fe | 34 | |
AustinKim | 0:8f61900631fe | 35 | #define WM01_APN_PROTOCOL_IPv4 1 |
AustinKim | 0:8f61900631fe | 36 | #define WM01_APN_PROTOCOL_IPv6 2 |
AustinKim | 0:8f61900631fe | 37 | #define WM01_DEFAULT_TIMEOUT 1000 |
AustinKim | 0:8f61900631fe | 38 | #define WM01_CONNECT_TIMEOUT 15000 |
AustinKim | 0:8f61900631fe | 39 | #define WM01_SEND_TIMEOUT 500 |
AustinKim | 0:8f61900631fe | 40 | #define WM01_RECV_TIMEOUT 500 |
AustinKim | 0:8f61900631fe | 41 | |
AustinKim | 0:8f61900631fe | 42 | #define WM01_APN_PROTOCOL WM01_APN_PROTOCOL_IPv6 |
AustinKim | 0:8f61900631fe | 43 | #define WM01_DEFAULT_BAUD_RATE 115200 |
AustinKim | 0:8f61900631fe | 44 | #define WM01_PARSER_DELIMITER "\r\n" |
AustinKim | 0:8f61900631fe | 45 | |
AustinKim | 0:8f61900631fe | 46 | #define CATM1_APN_SKT "lte-internet.sktelecom.com" |
AustinKim | 0:8f61900631fe | 47 | |
AustinKim | 0:8f61900631fe | 48 | #define CATM1_DEVICE_NAME_WM01 "WM01" |
AustinKim | 0:8f61900631fe | 49 | #define DEVNAME CATM1_DEVICE_NAME_WM01 |
AustinKim | 0:8f61900631fe | 50 | |
AustinKim | 0:8f61900631fe | 51 | #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } |
AustinKim | 0:8f61900631fe | 52 | #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} |
AustinKim | 0:8f61900631fe | 53 | |
AustinKim | 0:8f61900631fe | 54 | /* Pin configuraiton */ |
AustinKim | 0:8f61900631fe | 55 | // Cat.M1 |
AustinKim | 0:8f61900631fe | 56 | #define MBED_CONF_IOTSHIELD_CATM1_TX D8 |
AustinKim | 0:8f61900631fe | 57 | #define MBED_CONF_IOTSHIELD_CATM1_RX D2 |
AustinKim | 0:8f61900631fe | 58 | #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 |
AustinKim | 0:8f61900631fe | 59 | #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 |
AustinKim | 0:8f61900631fe | 60 | |
AustinKim | 0:8f61900631fe | 61 | // Sensors |
AustinKim | 0:8f61900631fe | 62 | #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 |
AustinKim | 0:8f61900631fe | 63 | #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 |
AustinKim | 0:8f61900631fe | 64 | |
AustinKim | 0:8f61900631fe | 65 | /* Debug message settings */ |
AustinKim | 0:8f61900631fe | 66 | #define WM01_PARSER_DEBUG DEBUG_DISABLE |
AustinKim | 0:8f61900631fe | 67 | #define CATM1_DEVICE_DEBUG DEBUG_ENABLE |
AustinKim | 0:8f61900631fe | 68 | |
AustinKim | 0:8f61900631fe | 69 | // Destination (Remote Host) |
AustinKim | 0:8f61900631fe | 70 | // IP address and Port number |
AustinKim | 2:2e1002e6798a | 71 | char dest_ip[] = "222.xxx.xxx.xxx"; |
AustinKim | 2:2e1002e6798a | 72 | int dest_port = 8000; |
AustinKim | 0:8f61900631fe | 73 | int protocol = 1; // 1 : TCP, 2 : UPD |
AustinKim | 0:8f61900631fe | 74 | int packet_type = 0; // 0 : ASCII, 1 : HEX, 2 : Binary |
AustinKim | 0:8f61900631fe | 75 | |
AustinKim | 0:8f61900631fe | 76 | // Functions: Print information |
AustinKim | 0:8f61900631fe | 77 | void printInfo(void); |
AustinKim | 0:8f61900631fe | 78 | |
AustinKim | 0:8f61900631fe | 79 | // Functions: Module Status |
AustinKim | 0:8f61900631fe | 80 | void waitCatM1Ready(void); |
AustinKim | 0:8f61900631fe | 81 | int8_t setEchoStatus_WM01(bool onoff); |
AustinKim | 0:8f61900631fe | 82 | int8_t getUsimStatus_WM01(void); |
AustinKim | 0:8f61900631fe | 83 | int8_t getNetworkStatus_WM01(void); |
AustinKim | 0:8f61900631fe | 84 | |
AustinKim | 0:8f61900631fe | 85 | // Functions: PDP context |
AustinKim | 0:8f61900631fe | 86 | int8_t setContextActivate_WM01(void); // Activate a PDP Context |
AustinKim | 0:8f61900631fe | 87 | int8_t setContextDeactivate_WM01(void); // Deactivate a PDP Context |
AustinKim | 0:8f61900631fe | 88 | |
AustinKim | 0:8f61900631fe | 89 | // Functions: TCP/UDP Socket service |
AustinKim | 0:8f61900631fe | 90 | int8_t sockOpenConnect_WM01(int protocol, const char *addr, int port, int pckttype); |
AustinKim | 0:8f61900631fe | 91 | int8_t sockClose_WM01(void); |
AustinKim | 0:8f61900631fe | 92 | int8_t sendData_WM01(char *data, int len); |
AustinKim | 0:8f61900631fe | 93 | int8_t recvData_WM01(char *data, int *len); |
AustinKim | 0:8f61900631fe | 94 | |
AustinKim | 0:8f61900631fe | 95 | Serial pc(USBTX, USBRX); // tx, rx |
AustinKim | 0:8f61900631fe | 96 | |
AustinKim | 0:8f61900631fe | 97 | UARTSerial *_serial; |
AustinKim | 0:8f61900631fe | 98 | ATCmdParser *_parser; |
AustinKim | 0:8f61900631fe | 99 | |
AustinKim | 0:8f61900631fe | 100 | DigitalOut _RESET_WM01(MBED_CONF_IOTSHIELD_CATM1_RESET); |
AustinKim | 0:8f61900631fe | 101 | DigitalOut _PWRKEY_WM01(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); |
AustinKim | 0:8f61900631fe | 102 | |
AustinKim | 0:8f61900631fe | 103 | void serialPcInit(void) |
AustinKim | 0:8f61900631fe | 104 | { |
AustinKim | 0:8f61900631fe | 105 | pc.baud(115200); |
AustinKim | 0:8f61900631fe | 106 | pc.format(8, Serial::None, 1); |
AustinKim | 0:8f61900631fe | 107 | } |
AustinKim | 0:8f61900631fe | 108 | |
AustinKim | 0:8f61900631fe | 109 | void serialDeviceInit(PinName tx, PinName rx, int baudrate) |
AustinKim | 0:8f61900631fe | 110 | { |
AustinKim | 0:8f61900631fe | 111 | _serial = new UARTSerial(tx, rx, baudrate); |
AustinKim | 0:8f61900631fe | 112 | } |
AustinKim | 0:8f61900631fe | 113 | |
AustinKim | 0:8f61900631fe | 114 | void serialAtParserInit(const char *delimiter, bool debug_en) |
AustinKim | 0:8f61900631fe | 115 | { |
AustinKim | 0:8f61900631fe | 116 | _parser = new ATCmdParser(_serial); |
AustinKim | 0:8f61900631fe | 117 | _parser->debug_on(debug_en); |
AustinKim | 0:8f61900631fe | 118 | _parser->set_delimiter(delimiter); |
AustinKim | 0:8f61900631fe | 119 | _parser->set_timeout(WM01_DEFAULT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 120 | } |
AustinKim | 0:8f61900631fe | 121 | |
AustinKim | 0:8f61900631fe | 122 | void catm1DeviceInit(void) |
AustinKim | 0:8f61900631fe | 123 | { |
AustinKim | 0:8f61900631fe | 124 | serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX, |
AustinKim | 0:8f61900631fe | 125 | MBED_CONF_IOTSHIELD_CATM1_RX, |
AustinKim | 0:8f61900631fe | 126 | WM01_DEFAULT_BAUD_RATE); |
AustinKim | 0:8f61900631fe | 127 | |
AustinKim | 0:8f61900631fe | 128 | serialAtParserInit( WM01_PARSER_DELIMITER, |
AustinKim | 0:8f61900631fe | 129 | WM01_PARSER_DEBUG); |
AustinKim | 0:8f61900631fe | 130 | } |
AustinKim | 0:8f61900631fe | 131 | |
AustinKim | 0:8f61900631fe | 132 | void catm1DeviceReset_WM01(void) |
AustinKim | 0:8f61900631fe | 133 | { |
AustinKim | 0:8f61900631fe | 134 | _RESET_WM01 = 1; |
AustinKim | 0:8f61900631fe | 135 | _PWRKEY_WM01 = 1; |
AustinKim | 0:8f61900631fe | 136 | wait_ms(300); |
AustinKim | 0:8f61900631fe | 137 | |
AustinKim | 0:8f61900631fe | 138 | _RESET_WM01 = 0; |
AustinKim | 0:8f61900631fe | 139 | _PWRKEY_WM01 = 0; |
AustinKim | 0:8f61900631fe | 140 | wait_ms(400); |
AustinKim | 0:8f61900631fe | 141 | |
AustinKim | 0:8f61900631fe | 142 | _RESET_WM01 = 1; |
AustinKim | 0:8f61900631fe | 143 | wait_ms(1000); |
AustinKim | 0:8f61900631fe | 144 | } |
AustinKim | 0:8f61900631fe | 145 | |
AustinKim | 0:8f61900631fe | 146 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 147 | // Main routine |
AustinKim | 0:8f61900631fe | 148 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 149 | |
AustinKim | 0:8f61900631fe | 150 | int main() |
AustinKim | 0:8f61900631fe | 151 | { |
AustinKim | 0:8f61900631fe | 152 | char sendbuf[] = "Hello Cat.M1\r\n"; |
AustinKim | 0:8f61900631fe | 153 | |
AustinKim | 0:8f61900631fe | 154 | serialPcInit(); |
AustinKim | 0:8f61900631fe | 155 | catm1DeviceInit(); |
AustinKim | 0:8f61900631fe | 156 | |
AustinKim | 0:8f61900631fe | 157 | myprintf("Waiting for Cat.M1 Module Ready...\r\n"); |
AustinKim | 0:8f61900631fe | 158 | |
AustinKim | 0:8f61900631fe | 159 | catm1DeviceReset_WM01(); |
AustinKim | 0:8f61900631fe | 160 | |
AustinKim | 0:8f61900631fe | 161 | waitCatM1Ready(); |
AustinKim | 0:8f61900631fe | 162 | |
AustinKim | 0:8f61900631fe | 163 | wait_ms(5000); |
AustinKim | 0:8f61900631fe | 164 | |
AustinKim | 0:8f61900631fe | 165 | myprintf("System Init Complete\r\n"); |
AustinKim | 0:8f61900631fe | 166 | |
AustinKim | 0:8f61900631fe | 167 | printInfo(); |
AustinKim | 0:8f61900631fe | 168 | |
AustinKim | 0:8f61900631fe | 169 | setEchoStatus_WM01(OFF); |
AustinKim | 0:8f61900631fe | 170 | |
AustinKim | 0:8f61900631fe | 171 | getUsimStatus_WM01(); |
AustinKim | 0:8f61900631fe | 172 | |
AustinKim | 0:8f61900631fe | 173 | getNetworkStatus_WM01(); |
AustinKim | 1:1c65b1dd9e7a | 174 | |
AustinKim | 1:1c65b1dd9e7a | 175 | wait_ms(3000); |
AustinKim | 1:1c65b1dd9e7a | 176 | |
AustinKim | 0:8f61900631fe | 177 | setContextActivate_WM01(); |
AustinKim | 0:8f61900631fe | 178 | |
AustinKim | 0:8f61900631fe | 179 | // TCP Client: Send and Receive |
AustinKim | 0:8f61900631fe | 180 | myprintf("TCP Client Start - Connect to %s:%d\r\n", dest_ip, dest_port); |
AustinKim | 0:8f61900631fe | 181 | |
AustinKim | 0:8f61900631fe | 182 | if(sockOpenConnect_WM01(protocol, dest_ip, dest_port, packet_type) == RET_OK) |
AustinKim | 0:8f61900631fe | 183 | { |
AustinKim | 1:1c65b1dd9e7a | 184 | myprintf("Socket Open Connect : success\r\n"); |
AustinKim | 0:8f61900631fe | 185 | |
AustinKim | 0:8f61900631fe | 186 | if(sendData_WM01(sendbuf, strlen(sendbuf))) |
AustinKim | 0:8f61900631fe | 187 | { |
AustinKim | 1:1c65b1dd9e7a | 188 | myprintf("Data Send : success [%d] %s\r\n", strlen(sendbuf), sendbuf); |
AustinKim | 0:8f61900631fe | 189 | } |
AustinKim | 0:8f61900631fe | 190 | else |
AustinKim | 0:8f61900631fe | 191 | { |
AustinKim | 1:1c65b1dd9e7a | 192 | myprintf("Data Send : failed\r\n"); |
AustinKim | 1:1c65b1dd9e7a | 193 | } |
AustinKim | 0:8f61900631fe | 194 | } |
AustinKim | 0:8f61900631fe | 195 | else |
AustinKim | 0:8f61900631fe | 196 | { |
AustinKim | 1:1c65b1dd9e7a | 197 | myprintf("Socket Open Connect : failed\r\n"); |
AustinKim | 0:8f61900631fe | 198 | |
AustinKim | 0:8f61900631fe | 199 | if(sockClose_WM01() == RET_OK) |
AustinKim | 0:8f61900631fe | 200 | { |
AustinKim | 1:1c65b1dd9e7a | 201 | myprintf("Socket Close : success\r\n"); |
AustinKim | 0:8f61900631fe | 202 | } |
AustinKim | 0:8f61900631fe | 203 | else |
AustinKim | 0:8f61900631fe | 204 | { |
AustinKim | 1:1c65b1dd9e7a | 205 | myprintf("Socket Close : failed\r\n"); |
AustinKim | 0:8f61900631fe | 206 | } |
AustinKim | 0:8f61900631fe | 207 | } |
AustinKim | 0:8f61900631fe | 208 | |
AustinKim | 0:8f61900631fe | 209 | while(1) |
AustinKim | 0:8f61900631fe | 210 | { |
AustinKim | 0:8f61900631fe | 211 | // Data received |
AustinKim | 0:8f61900631fe | 212 | char recvbuf[100] = {0, }; |
AustinKim | 0:8f61900631fe | 213 | int recvlen = 0; |
AustinKim | 1:1c65b1dd9e7a | 214 | |
AustinKim | 0:8f61900631fe | 215 | if(recvData_WM01(recvbuf, &recvlen) == RET_OK) |
AustinKim | 0:8f61900631fe | 216 | { |
AustinKim | 1:1c65b1dd9e7a | 217 | myprintf("Data Receive [%d] %s\r\n", recvlen, recvbuf); |
AustinKim | 0:8f61900631fe | 218 | |
AustinKim | 0:8f61900631fe | 219 | char *ptr = strstr(recvbuf, "exit"); |
AustinKim | 0:8f61900631fe | 220 | |
AustinKim | 0:8f61900631fe | 221 | if(ptr != 0) |
AustinKim | 0:8f61900631fe | 222 | { |
AustinKim | 0:8f61900631fe | 223 | break; |
AustinKim | 0:8f61900631fe | 224 | } |
AustinKim | 0:8f61900631fe | 225 | } |
AustinKim | 0:8f61900631fe | 226 | } |
AustinKim | 0:8f61900631fe | 227 | |
AustinKim | 0:8f61900631fe | 228 | if(sockClose_WM01() == RET_OK) |
AustinKim | 0:8f61900631fe | 229 | { |
AustinKim | 1:1c65b1dd9e7a | 230 | myprintf("Socket Close : success\r\n"); |
AustinKim | 0:8f61900631fe | 231 | } |
AustinKim | 0:8f61900631fe | 232 | else |
AustinKim | 0:8f61900631fe | 233 | { |
AustinKim | 1:1c65b1dd9e7a | 234 | myprintf("Socket Close : failed\r\n"); |
AustinKim | 0:8f61900631fe | 235 | } |
AustinKim | 0:8f61900631fe | 236 | |
AustinKim | 0:8f61900631fe | 237 | setContextDeactivate_WM01(); |
AustinKim | 0:8f61900631fe | 238 | } |
AustinKim | 0:8f61900631fe | 239 | |
AustinKim | 0:8f61900631fe | 240 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 241 | // Functions: Print information |
AustinKim | 0:8f61900631fe | 242 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 243 | |
AustinKim | 0:8f61900631fe | 244 | void printInfo(void) |
AustinKim | 0:8f61900631fe | 245 | { |
AustinKim | 0:8f61900631fe | 246 | myprintf("WIZnet IoT Shield for Arm MBED"); |
AustinKim | 0:8f61900631fe | 247 | myprintf("LTE Cat.M1 Version"); |
AustinKim | 0:8f61900631fe | 248 | myprintf("================================================="); |
AustinKim | 0:8f61900631fe | 249 | myprintf(">> Target Board: WIoT-WM01 (Woorinet WM-N400MSE)"); |
AustinKim | 1:1c65b1dd9e7a | 250 | myprintf(">> Sample Code: TCP Test"); |
AustinKim | 0:8f61900631fe | 251 | myprintf("=================================================\r\n"); |
AustinKim | 0:8f61900631fe | 252 | } |
AustinKim | 0:8f61900631fe | 253 | |
AustinKim | 0:8f61900631fe | 254 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 255 | // Functions: Cat.M1 Status |
AustinKim | 0:8f61900631fe | 256 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 257 | |
AustinKim | 0:8f61900631fe | 258 | void waitCatM1Ready(void) |
AustinKim | 0:8f61900631fe | 259 | { |
AustinKim | 0:8f61900631fe | 260 | while(1) |
AustinKim | 0:8f61900631fe | 261 | { |
AustinKim | 0:8f61900631fe | 262 | if(_parser->send("AT") && _parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 263 | { |
AustinKim | 0:8f61900631fe | 264 | myprintf("WM01 is Available\r\n"); |
AustinKim | 0:8f61900631fe | 265 | |
AustinKim | 0:8f61900631fe | 266 | return; |
AustinKim | 0:8f61900631fe | 267 | } |
AustinKim | 1:1c65b1dd9e7a | 268 | } |
AustinKim | 0:8f61900631fe | 269 | } |
AustinKim | 0:8f61900631fe | 270 | |
AustinKim | 0:8f61900631fe | 271 | int8_t setEchoStatus_WM01(bool onoff) |
AustinKim | 0:8f61900631fe | 272 | { |
AustinKim | 0:8f61900631fe | 273 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 274 | char _buf[10]; |
AustinKim | 0:8f61900631fe | 275 | |
AustinKim | 0:8f61900631fe | 276 | sprintf((char *)_buf, "ATE%d", onoff); |
AustinKim | 0:8f61900631fe | 277 | |
AustinKim | 0:8f61900631fe | 278 | if(_parser->send(_buf) && _parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 279 | { |
AustinKim | 1:1c65b1dd9e7a | 280 | devlog("Turn Echo %s : success\r\n", onoff?"ON":"OFF"); |
AustinKim | 0:8f61900631fe | 281 | |
AustinKim | 0:8f61900631fe | 282 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 283 | } |
AustinKim | 0:8f61900631fe | 284 | else |
AustinKim | 0:8f61900631fe | 285 | { |
AustinKim | 1:1c65b1dd9e7a | 286 | devlog("Turn Echo %s : failed\r\n", onoff?"ON":"OFF"); |
AustinKim | 0:8f61900631fe | 287 | } |
AustinKim | 0:8f61900631fe | 288 | |
AustinKim | 0:8f61900631fe | 289 | return ret; |
AustinKim | 0:8f61900631fe | 290 | } |
AustinKim | 0:8f61900631fe | 291 | |
AustinKim | 0:8f61900631fe | 292 | int8_t getUsimStatus_WM01(void) |
AustinKim | 0:8f61900631fe | 293 | { |
AustinKim | 0:8f61900631fe | 294 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 295 | |
AustinKim | 0:8f61900631fe | 296 | if(_parser->send("AT$$STAT?") && _parser->recv("$$STAT:READY") && _parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 297 | { |
AustinKim | 1:1c65b1dd9e7a | 298 | devlog("USIM Status : READY\r\n"); |
AustinKim | 0:8f61900631fe | 299 | |
AustinKim | 0:8f61900631fe | 300 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 301 | } |
AustinKim | 0:8f61900631fe | 302 | else |
AustinKim | 0:8f61900631fe | 303 | { |
AustinKim | 0:8f61900631fe | 304 | devlog("Retrieving USIM Status failed\r\n"); |
AustinKim | 0:8f61900631fe | 305 | } |
AustinKim | 0:8f61900631fe | 306 | |
AustinKim | 0:8f61900631fe | 307 | return ret; |
AustinKim | 0:8f61900631fe | 308 | } |
AustinKim | 0:8f61900631fe | 309 | |
AustinKim | 0:8f61900631fe | 310 | int8_t getNetworkStatus_WM01(void) |
AustinKim | 0:8f61900631fe | 311 | { |
AustinKim | 0:8f61900631fe | 312 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 313 | int val, stat; |
AustinKim | 0:8f61900631fe | 314 | |
AustinKim | 0:8f61900631fe | 315 | if(_parser->send("AT+CEREG?") && _parser->recv("+CEREG: %d,%d", &val, &stat) && _parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 316 | { |
AustinKim | 0:8f61900631fe | 317 | if((val == 0) && (stat == 1)) |
AustinKim | 0:8f61900631fe | 318 | { |
AustinKim | 1:1c65b1dd9e7a | 319 | devlog("Network Status : attached\r\n"); |
AustinKim | 0:8f61900631fe | 320 | |
AustinKim | 0:8f61900631fe | 321 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 322 | } |
AustinKim | 0:8f61900631fe | 323 | else |
AustinKim | 0:8f61900631fe | 324 | { |
AustinKim | 1:1c65b1dd9e7a | 325 | devlog("Network Status : %d, %d\r\n", val, stat); |
AustinKim | 0:8f61900631fe | 326 | } |
AustinKim | 0:8f61900631fe | 327 | } |
AustinKim | 0:8f61900631fe | 328 | else |
AustinKim | 0:8f61900631fe | 329 | { |
AustinKim | 1:1c65b1dd9e7a | 330 | devlog("Network Status : Error\r\n"); |
AustinKim | 0:8f61900631fe | 331 | } |
AustinKim | 0:8f61900631fe | 332 | |
AustinKim | 0:8f61900631fe | 333 | return ret; |
AustinKim | 0:8f61900631fe | 334 | } |
AustinKim | 0:8f61900631fe | 335 | |
AustinKim | 0:8f61900631fe | 336 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 337 | // Functions: Cat.M1 PDP context activate / deactivate |
AustinKim | 0:8f61900631fe | 338 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 339 | |
AustinKim | 0:8f61900631fe | 340 | int8_t setContextActivate_WM01(void) // Activate a PDP Context |
AustinKim | 0:8f61900631fe | 341 | { |
AustinKim | 0:8f61900631fe | 342 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 343 | |
AustinKim | 0:8f61900631fe | 344 | _parser->send("AT*RNDISDATA=1"); |
AustinKim | 0:8f61900631fe | 345 | |
AustinKim | 0:8f61900631fe | 346 | if(_parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 347 | { |
AustinKim | 0:8f61900631fe | 348 | devlog("Activate a PDP Context\r\n"); |
AustinKim | 0:8f61900631fe | 349 | |
AustinKim | 0:8f61900631fe | 350 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 351 | } |
AustinKim | 0:8f61900631fe | 352 | else |
AustinKim | 0:8f61900631fe | 353 | { |
AustinKim | 0:8f61900631fe | 354 | devlog("PDP Context Activation failed\r\n"); |
AustinKim | 0:8f61900631fe | 355 | } |
AustinKim | 0:8f61900631fe | 356 | |
AustinKim | 0:8f61900631fe | 357 | return ret; |
AustinKim | 0:8f61900631fe | 358 | } |
AustinKim | 0:8f61900631fe | 359 | |
AustinKim | 0:8f61900631fe | 360 | int8_t setContextDeactivate_WM01(void) // Deactivate a PDP Context |
AustinKim | 0:8f61900631fe | 361 | { |
AustinKim | 0:8f61900631fe | 362 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 363 | |
AustinKim | 0:8f61900631fe | 364 | _parser->send("AT*RNDISDATA=0"); |
AustinKim | 0:8f61900631fe | 365 | |
AustinKim | 0:8f61900631fe | 366 | if(_parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 367 | { |
AustinKim | 0:8f61900631fe | 368 | devlog("Deactivate a PDP Context\r\n"); |
AustinKim | 0:8f61900631fe | 369 | |
AustinKim | 0:8f61900631fe | 370 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 371 | } |
AustinKim | 0:8f61900631fe | 372 | else |
AustinKim | 0:8f61900631fe | 373 | { |
AustinKim | 0:8f61900631fe | 374 | devlog("PDP Context Deactivation failed\r\n"); |
AustinKim | 0:8f61900631fe | 375 | } |
AustinKim | 1:1c65b1dd9e7a | 376 | |
AustinKim | 0:8f61900631fe | 377 | return ret; |
AustinKim | 0:8f61900631fe | 378 | } |
AustinKim | 0:8f61900631fe | 379 | |
AustinKim | 0:8f61900631fe | 380 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 381 | // Functions: TCP/UDP socket service |
AustinKim | 0:8f61900631fe | 382 | // ---------------------------------------------------------------- |
AustinKim | 0:8f61900631fe | 383 | |
AustinKim | 0:8f61900631fe | 384 | int8_t sockOpenConnect_WM01(int protocol, const char *addr, int port, int pckttype) |
AustinKim | 0:8f61900631fe | 385 | { |
AustinKim | 0:8f61900631fe | 386 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 387 | bool done = false; |
AustinKim | 0:8f61900631fe | 388 | int ok = 0; // 0 : failure , 1 : success |
AustinKim | 0:8f61900631fe | 389 | int id = 0; |
AustinKim | 0:8f61900631fe | 390 | |
AustinKim | 0:8f61900631fe | 391 | Timer t; |
AustinKim | 0:8f61900631fe | 392 | |
AustinKim | 0:8f61900631fe | 393 | _parser->set_timeout(WM01_CONNECT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 394 | |
AustinKim | 0:8f61900631fe | 395 | if((protocol != 0) && (protocol != 1)) // 0 : TCP, 1 : UDP |
AustinKim | 0:8f61900631fe | 396 | { |
AustinKim | 0:8f61900631fe | 397 | return RET_NOK; |
AustinKim | 0:8f61900631fe | 398 | } |
AustinKim | 0:8f61900631fe | 399 | |
AustinKim | 0:8f61900631fe | 400 | t.start(); |
AustinKim | 0:8f61900631fe | 401 | |
AustinKim | 0:8f61900631fe | 402 | _parser->send("AT+WSOCR=%d,%s,%d,%d,%d", id, addr, port, protocol, pckttype); |
AustinKim | 0:8f61900631fe | 403 | |
AustinKim | 0:8f61900631fe | 404 | do |
AustinKim | 0:8f61900631fe | 405 | { |
AustinKim | 0:8f61900631fe | 406 | done = (_parser->recv("+WSOCR:%d,%d", &ok,&id) && (ok == 1)); |
AustinKim | 0:8f61900631fe | 407 | } while(!done && t.read_ms() < WM01_CONNECT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 408 | |
AustinKim | 0:8f61900631fe | 409 | if(done) |
AustinKim | 0:8f61900631fe | 410 | { |
AustinKim | 0:8f61900631fe | 411 | if(_parser->send("AT+WSOCO=%d", id) && _parser->recv("+WSOCO:%d,%d,OPEN_WAIT", &ok, &id) && _parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 412 | { |
AustinKim | 0:8f61900631fe | 413 | if(ok == 1) |
AustinKim | 0:8f61900631fe | 414 | { |
AustinKim | 0:8f61900631fe | 415 | if(_parser->recv("+WSOCO:%d,OPEN_CMPL", &id)) |
AustinKim | 0:8f61900631fe | 416 | { |
AustinKim | 0:8f61900631fe | 417 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 418 | } |
AustinKim | 0:8f61900631fe | 419 | } |
AustinKim | 0:8f61900631fe | 420 | } |
AustinKim | 0:8f61900631fe | 421 | } |
AustinKim | 0:8f61900631fe | 422 | |
AustinKim | 0:8f61900631fe | 423 | _parser->set_timeout(WM01_DEFAULT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 424 | _parser->flush(); |
AustinKim | 0:8f61900631fe | 425 | |
AustinKim | 0:8f61900631fe | 426 | return ret; |
AustinKim | 0:8f61900631fe | 427 | } |
AustinKim | 0:8f61900631fe | 428 | |
AustinKim | 0:8f61900631fe | 429 | int8_t sockClose_WM01(void) |
AustinKim | 0:8f61900631fe | 430 | { |
AustinKim | 0:8f61900631fe | 431 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 432 | int ok = 0; // 0 : failure , 1 : success |
AustinKim | 0:8f61900631fe | 433 | int id = 0; |
AustinKim | 0:8f61900631fe | 434 | |
AustinKim | 0:8f61900631fe | 435 | _parser->set_timeout(WM01_CONNECT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 436 | |
AustinKim | 0:8f61900631fe | 437 | if(_parser->send("AT+WSOCL=%d", id) && _parser->recv("+WSOCL:%d,%d,CLOSE_WAIT", &ok, &id) &&_parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 438 | { |
AustinKim | 0:8f61900631fe | 439 | if(ok == 1) |
AustinKim | 0:8f61900631fe | 440 | { |
AustinKim | 0:8f61900631fe | 441 | ret = RET_OK; |
AustinKim | 1:1c65b1dd9e7a | 442 | } |
AustinKim | 0:8f61900631fe | 443 | } |
AustinKim | 1:1c65b1dd9e7a | 444 | |
AustinKim | 0:8f61900631fe | 445 | _parser->set_timeout(WM01_DEFAULT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 446 | |
AustinKim | 0:8f61900631fe | 447 | return ret; |
AustinKim | 0:8f61900631fe | 448 | } |
AustinKim | 0:8f61900631fe | 449 | |
AustinKim | 0:8f61900631fe | 450 | int8_t sendData_WM01(char *data, int len) |
AustinKim | 0:8f61900631fe | 451 | { |
AustinKim | 0:8f61900631fe | 452 | int8_t ret = RET_NOK; |
AustinKim | 0:8f61900631fe | 453 | bool done = false; |
AustinKim | 0:8f61900631fe | 454 | int ok = 0; // 0 : failure , 1 : success |
AustinKim | 0:8f61900631fe | 455 | int id = 0; |
AustinKim | 0:8f61900631fe | 456 | |
AustinKim | 0:8f61900631fe | 457 | _parser->set_timeout(WM01_SEND_TIMEOUT); |
AustinKim | 0:8f61900631fe | 458 | _parser->send("AT+WSOWR=%d,%d,%s", id, len, data); |
AustinKim | 0:8f61900631fe | 459 | |
AustinKim | 0:8f61900631fe | 460 | if(_parser->recv("+WSOWR:%d,%d", &ok, &id) && _parser->recv("OK")) |
AustinKim | 0:8f61900631fe | 461 | { |
AustinKim | 0:8f61900631fe | 462 | if(ok == 1) // success |
AustinKim | 0:8f61900631fe | 463 | { |
AustinKim | 0:8f61900631fe | 464 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 465 | } |
AustinKim | 0:8f61900631fe | 466 | } |
AustinKim | 0:8f61900631fe | 467 | |
AustinKim | 0:8f61900631fe | 468 | _parser->set_timeout(WM01_DEFAULT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 469 | |
AustinKim | 0:8f61900631fe | 470 | return ret; |
AustinKim | 0:8f61900631fe | 471 | } |
AustinKim | 0:8f61900631fe | 472 | |
AustinKim | 0:8f61900631fe | 473 | int8_t recvData_WM01(char *data, int *len) |
AustinKim | 0:8f61900631fe | 474 | { |
AustinKim | 0:8f61900631fe | 475 | int8_t ret = RET_NOK; |
AustinKim | 1:1c65b1dd9e7a | 476 | char _buf[100] = {0, }; |
AustinKim | 0:8f61900631fe | 477 | int id = 0; |
AustinKim | 1:1c65b1dd9e7a | 478 | int i = 0; |
AustinKim | 1:1c65b1dd9e7a | 479 | |
AustinKim | 1:1c65b1dd9e7a | 480 | Timer t; |
AustinKim | 0:8f61900631fe | 481 | |
AustinKim | 0:8f61900631fe | 482 | _parser->set_timeout(WM01_RECV_TIMEOUT); |
AustinKim | 0:8f61900631fe | 483 | |
AustinKim | 1:1c65b1dd9e7a | 484 | t.start(); |
AustinKim | 1:1c65b1dd9e7a | 485 | |
AustinKim | 1:1c65b1dd9e7a | 486 | while(t.read_ms() < WM01_RECV_TIMEOUT) |
AustinKim | 1:1c65b1dd9e7a | 487 | { |
AustinKim | 1:1c65b1dd9e7a | 488 | _buf[i] = _parser->getc(); |
AustinKim | 1:1c65b1dd9e7a | 489 | |
AustinKim | 1:1c65b1dd9e7a | 490 | i++; |
AustinKim | 1:1c65b1dd9e7a | 491 | } |
AustinKim | 1:1c65b1dd9e7a | 492 | |
AustinKim | 1:1c65b1dd9e7a | 493 | if(sscanf(_buf,"+WSORD:%d,%d,%[^\n]", &id, len, data)) |
AustinKim | 0:8f61900631fe | 494 | { |
AustinKim | 0:8f61900631fe | 495 | ret = RET_OK; |
AustinKim | 0:8f61900631fe | 496 | } |
AustinKim | 0:8f61900631fe | 497 | |
AustinKim | 0:8f61900631fe | 498 | _parser->set_timeout(WM01_DEFAULT_TIMEOUT); |
AustinKim | 0:8f61900631fe | 499 | _parser->flush(); |
AustinKim | 0:8f61900631fe | 500 | |
AustinKim | 0:8f61900631fe | 501 | return ret; |
AustinKim | 0:8f61900631fe | 502 | } |