Eric Jung / Mbed OS WIZnet-IoTShield-BG96-TCP

Files at this revision

API Documentation at this revision

Comitter:
hkjung
Date:
Wed Mar 20 05:31:05 2019 +0000
Parent:
1:35abb05061e0
Child:
3:4041e2cc0f19
Commit message:
Added license identifier and some comment

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Mar 16 09:04:43 2019 +0000
+++ b/main.cpp	Wed Mar 20 05:31:05 2019 +0000
@@ -2,6 +2,23 @@
  * Copyright (c) 2019 WIZnet Co., Ltd.
  * SPDX-License-Identifier: Apache-2.0
  */
+ 
+ /* 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
 
 #include <string>
 #include "mbed.h"
@@ -39,9 +56,9 @@
 // Cat.M1
 #define MBED_CONF_IOTSHIELD_CATM1_TX                D8
 #define MBED_CONF_IOTSHIELD_CATM1_RX                D2
-#define MBED_CONF_IOTSHIELD_CATM1_RESET             D6
+#define MBED_CONF_IOTSHIELD_CATM1_RESET             D7
 #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY            D9
-#define MBED_CONF_IOTSHIELD_CATM1_WAKE              NC
+
 // Sensors
 #define MBED_CONF_IOTSHIELD_SENSOR_CDS              A0
 #define MBED_CONF_IOTSHIELD_SENSOR_TEMP             A1
@@ -50,23 +67,29 @@
 #define BG96_PARSER_DEBUG           DEBUG_DISABLE
 #define CATM1_DEVICE_DEBUG          DEBUG_ENABLE 
 
-
+// Functions: Module Status
+void waitCatM1Ready(void);
 int8_t setEchoStatus_BG96(bool onoff);
 int8_t getUsimStatus_BG96(void);
 int8_t getNetworkStatus_BG96(void);
 int8_t checknSetApn_BG96(const char * apn);
 int8_t getFirmwareVersion_BG96(char * version);
 int8_t getImeiNumber_BG96(char * imei);
-int8_t getIpAddressByName_BG96(const char * name, char * ipstr); // DNS
 
+// Functions: DNS
+int8_t getIpAddressByName_BG96(const char * name, char * ipstr);
+
+// Functions: PDP context
 int8_t setContextActivate_BG96(void);   // Activate a PDP Context
 int8_t setContextDeactivate_BG96(void); // Deactivate a PDP Context
 int8_t getIpAddress_BG96(char * ipstr);
+
+// Functions: TCP/UDP Socket service
 int8_t sockOpenConnect_BG96(const char * type, const char * addr, int port);
 int8_t sockClose_BG96(void);
-int8_t dataSend_BG96(char * data, int len);
-int8_t checkDataRecv_BG96(void);
-int8_t dataRecv_BG96(char * data, int * len);
+int8_t sendData_BG96(char * data, int len);
+int8_t checkRecvData_BG96(void);
+int8_t recvData_BG96(char * data, int * len);
 
 
 Serial pc(USBTX, USBRX); // tx, rx
@@ -74,10 +97,12 @@
 UARTSerial *_serial;
 ATCmdParser *_parser;
 
+DigitalOut _RESET_BG96(MBED_CONF_IOTSHIELD_CATM1_RESET);
+DigitalOut _PWRKEY_BG96(MBED_CONF_IOTSHIELD_CATM1_PWRKEY);
 
 // Destination (Remote Host)
 // IP address and Port number
-char dest_ip[] = "222.98.173.214";
+char dest_ip[] = "222.xxx.xxx.xxx";
 int  dest_port = 50001;
 
 
@@ -110,23 +135,23 @@
                         BG96_PARSER_DEBUG);
 }
 
-void waitCatM1Ready(void)
+void catm1DeviceReset_BG96(void)
 {
-    while(1) 
-    {   
-        if(_parser->recv("RDY")) 
-        {
-            myprintf("BG96 ready\r\n");
-            return ;
-        }
-        else if(_parser->send("AT") && _parser->recv("OK"))
-        {
-            myprintf("BG96 already available\r\n");
-            return ;
-        }        
-    }        
+    _RESET_BG96 = 1;
+    _PWRKEY_BG96 = 1;
+    wait_ms(300);
+    
+    _RESET_BG96 = 0;
+    _PWRKEY_BG96 = 0;
+    wait_ms(400);
+    
+    _RESET_BG96 = 1;    
+    wait_ms(1000);
 }
 
+// ----------------------------------------------------------------
+// Main routine
+// ----------------------------------------------------------------
 
 int main()
 {
@@ -135,9 +160,11 @@
     
     myprintf("Waiting for Cat.M1 Module Ready...\r\n");
     
-    // Todo: BG96 Hardware Init (hardware reset & pwrkey act)
+    catm1DeviceReset_BG96();
     
     waitCatM1Ready();
+    
+    wait_ms(5000);
             
     myprintf("System Init Complete\r\n");
     
@@ -157,6 +184,17 @@
     checknSetApn_BG96(CATM1_APN_SKT);
    
     setContextActivate_BG96();
+        
+#if 0    
+    // DNS Sample
+    const char dest_domain[] = "www.google.com";
+    char ipstr[50];
+    if(getIpAddressByName_BG96(dest_domain, ipstr)) {
+        myprintf("DNS: %s [%s]\r\n", ipstr, dest_domain);
+    } else {
+        myprintf("DNS: failed\r\n");
+    }
+#endif    
 
     // TCP Client: Send and Receive
     myprintf("TCP Client Start - Connect to %s:%d\r\n", dest_ip, dest_port);
@@ -165,7 +203,7 @@
         myprintf("sockOpenConnect: success\r\n");
         
         char sendbuf[] = "Hello Cat.M1\r\n";         
-        if(dataSend_BG96(sendbuf, sizeof(sendbuf))) {
+        if(sendData_BG96(sendbuf, sizeof(sendbuf))) {
             myprintf("dataSend [%d] %s\r\n", sizeof(sendbuf), sendbuf);
         }
         
@@ -181,12 +219,12 @@
     
     while(1)
     {
-        if(checkDataRecv_BG96() == RET_OK) {
+        if(checkRecvData_BG96() == RET_OK) {
             // Data received
             char recvbuf[100] = {0, };
             int recvlen = 0;
             
-            if(dataRecv_BG96(recvbuf, &recvlen) == RET_OK) {
+            if(recvData_BG96(recvbuf, &recvlen) == RET_OK) {
                 myprintf("dataRecv [%d] %s\r\n", recvlen, recvbuf);
                 
                 char * ptr = strstr(recvbuf, "exit");
@@ -202,7 +240,26 @@
     setContextDeactivate_BG96(); 
 }
 
+// ----------------------------------------------------------------
+// Functions: Cat.M1 Status
+// ----------------------------------------------------------------
 
+void waitCatM1Ready(void)
+{
+    while(1) 
+    {   
+        if(_parser->recv("RDY")) 
+        {
+            myprintf("BG96 ready\r\n");
+            return ;
+        }
+        else if(_parser->send("AT") && _parser->recv("OK"))
+        {
+            myprintf("BG96 already available\r\n");
+            return ;
+        }        
+    }        
+}
 
 int8_t setEchoStatus_BG96(bool onoff)
 {
@@ -334,18 +391,9 @@
     return ret;           
 }
 
-int8_t getIpAddress_BG96(char * ipstr) // IPv4 or IPv6
-{
-    int8_t ret = RET_NOK;    
-    int id, state, type; // not used    
-
-    _parser->send("AT+QIACT?");
-    if(_parser->recv("+QIACT: %d,%d,%d,\"%[^\"]\"", &id, &state, &type, ipstr)
-        && _parser->recv("OK")) {        
-        ret = RET_OK;
-    } 
-    return ret;
-}
+// ----------------------------------------------------------------
+// Functions: Cat.M1 PDP context activate / deactivate
+// ----------------------------------------------------------------
 
 int8_t setContextActivate_BG96(void) // Activate a PDP Context
 {
@@ -375,6 +423,23 @@
     return ret;
 }
 
+int8_t getIpAddress_BG96(char * ipstr) // IPv4 or IPv6
+{
+    int8_t ret = RET_NOK;    
+    int id, state, type; // not used    
+
+    _parser->send("AT+QIACT?");
+    if(_parser->recv("+QIACT: %d,%d,%d,\"%[^\"]\"", &id, &state, &type, ipstr)
+        && _parser->recv("OK")) {        
+        ret = RET_OK;
+    } 
+    return ret;
+}
+
+// ----------------------------------------------------------------
+// Functions: TCP/UDP socket service
+// ----------------------------------------------------------------
+
 int8_t sockOpenConnect_BG96(const char * type, const char * addr, int port)
 {
     int8_t ret = RET_NOK;  
@@ -420,7 +485,7 @@
     return ret;
 }
 
-int8_t dataSend_BG96(char * data, int len)
+int8_t sendData_BG96(char * data, int len)
 {
     int8_t ret = RET_NOK;
     int id = 0;
@@ -440,7 +505,7 @@
     return ret;
 }
 
-int8_t checkDataRecv_BG96(void)
+int8_t checkRecvData_BG96(void)
 {
     int8_t ret = RET_NOK;
     int id = 0;
@@ -457,7 +522,7 @@
     return ret;
 }
 
-int8_t dataRecv_BG96(char * data, int * len)
+int8_t recvData_BG96(char * data, int * len)
 {
     int8_t ret = RET_NOK;
     int id = 0;