tes sim 5360

Committer:
irsanjul
Date:
Tue Mar 17 09:22:08 2020 +0000
Revision:
0:bf27ed7867b7
tes sim 5360

Who changed what in which revision?

UserRevisionLine numberNew contents of line
irsanjul 0:bf27ed7867b7 1 #ifndef _GSM_H_
irsanjul 0:bf27ed7867b7 2 #define _GSM_H_
irsanjul 0:bf27ed7867b7 3
irsanjul 0:bf27ed7867b7 4 #include <stdio.h>
irsanjul 0:bf27ed7867b7 5 #include "mbed.h"
irsanjul 0:bf27ed7867b7 6
irsanjul 0:bf27ed7867b7 7 #define DEFAULT_TIMEOUT 5
irsanjul 0:bf27ed7867b7 8 #define SMS_MAX_LENGTH 16
irsanjul 0:bf27ed7867b7 9
irsanjul 0:bf27ed7867b7 10
irsanjul 0:bf27ed7867b7 11 enum GSM_MESSAGE {
irsanjul 0:bf27ed7867b7 12 MESSAGE_RING = 0,
irsanjul 0:bf27ed7867b7 13 MESSAGE_SMS = 1,
irsanjul 0:bf27ed7867b7 14 MESSAGE_ERROR
irsanjul 0:bf27ed7867b7 15 };
irsanjul 0:bf27ed7867b7 16
irsanjul 0:bf27ed7867b7 17
irsanjul 0:bf27ed7867b7 18 /** GSM class.
irsanjul 0:bf27ed7867b7 19 * Used for mobile communication. attention that GSM module communicate with MCU in serial protocol
irsanjul 0:bf27ed7867b7 20 */
irsanjul 0:bf27ed7867b7 21 class GSM
irsanjul 0:bf27ed7867b7 22 {
irsanjul 0:bf27ed7867b7 23 public:
irsanjul 0:bf27ed7867b7 24 /** Create GSM instance
irsanjul 0:bf27ed7867b7 25 * @param tx uart transmit pin to communicate with GSM module
irsanjul 0:bf27ed7867b7 26 * @param rx uart receive pin to communicate with GSM module
irsanjul 0:bf27ed7867b7 27 * @param baudRate baud rate of uart communication
irsanjul 0:bf27ed7867b7 28 * @param number default phone number during mobile communication
irsanjul 0:bf27ed7867b7 29 */
irsanjul 0:bf27ed7867b7 30 GSM(PinName tx, PinName rx, int baudRate,char *number) : gprsSerial(tx, rx) {
irsanjul 0:bf27ed7867b7 31 //gprsSerial.baud(baudRate);
irsanjul 0:bf27ed7867b7 32 phoneNumber = number;
irsanjul 0:bf27ed7867b7 33 };
irsanjul 0:bf27ed7867b7 34
irsanjul 0:bf27ed7867b7 35 int powerCheck(void);
irsanjul 0:bf27ed7867b7 36
irsanjul 0:bf27ed7867b7 37 /** init GSM module including SIM card check & signal strength & network check
irsanjul 0:bf27ed7867b7 38 * @returns
irsanjul 0:bf27ed7867b7 39 * 0 on success,
irsanjul 0:bf27ed7867b7 40 * -1 on error
irsanjul 0:bf27ed7867b7 41 */
irsanjul 0:bf27ed7867b7 42 int init(void);
irsanjul 0:bf27ed7867b7 43
irsanjul 0:bf27ed7867b7 44 /** Register Network of GSM module including signal strength & network check
irsanjul 0:bf27ed7867b7 45 * @returns
irsanjul 0:bf27ed7867b7 46 * 0 on success,
irsanjul 0:bf27ed7867b7 47 * -1 on error
irsanjul 0:bf27ed7867b7 48 */
irsanjul 0:bf27ed7867b7 49 int registerNet(void);
irsanjul 0:bf27ed7867b7 50
irsanjul 0:bf27ed7867b7 51 /** Check network status of GSM module
irsanjul 0:bf27ed7867b7 52 * @returns
irsanjul 0:bf27ed7867b7 53 * 0 on success,
irsanjul 0:bf27ed7867b7 54 * -1 on error
irsanjul 0:bf27ed7867b7 55 */
irsanjul 0:bf27ed7867b7 56 int checkNetStatus(void);
irsanjul 0:bf27ed7867b7 57
irsanjul 0:bf27ed7867b7 58 /** Check SIM card' Status
irsanjul 0:bf27ed7867b7 59 * @returns
irsanjul 0:bf27ed7867b7 60 * 0 on success,
irsanjul 0:bf27ed7867b7 61 * -1 on error
irsanjul 0:bf27ed7867b7 62 */
irsanjul 0:bf27ed7867b7 63 int checkSIMStatus(void);
irsanjul 0:bf27ed7867b7 64
irsanjul 0:bf27ed7867b7 65 /** Check signal strength
irsanjul 0:bf27ed7867b7 66 * @returns
irsanjul 0:bf27ed7867b7 67 * signal strength in number(ex 3,4,5,6,7,8...) on success,
irsanjul 0:bf27ed7867b7 68 * -1 on error
irsanjul 0:bf27ed7867b7 69 */
irsanjul 0:bf27ed7867b7 70 int checkSignalStrength(void);
irsanjul 0:bf27ed7867b7 71
irsanjul 0:bf27ed7867b7 72 /** Set SMS format and processing mode
irsanjul 0:bf27ed7867b7 73 * @returns
irsanjul 0:bf27ed7867b7 74 * 0 on success,
irsanjul 0:bf27ed7867b7 75 * -1 on error
irsanjul 0:bf27ed7867b7 76 */
irsanjul 0:bf27ed7867b7 77 int settingSMS(void);
irsanjul 0:bf27ed7867b7 78
irsanjul 0:bf27ed7867b7 79 /** Send text SMS
irsanjul 0:bf27ed7867b7 80 * @param *number phone number which SMS will be send to
irsanjul 0:bf27ed7867b7 81 * @param *data message that will be send to
irsanjul 0:bf27ed7867b7 82 * @returns
irsanjul 0:bf27ed7867b7 83 * 0 on success,
irsanjul 0:bf27ed7867b7 84 * -1 on error
irsanjul 0:bf27ed7867b7 85 */
irsanjul 0:bf27ed7867b7 86 int sendSMS(char *number, char *data);
irsanjul 0:bf27ed7867b7 87
irsanjul 0:bf27ed7867b7 88 /** Read SMS by index
irsanjul 0:bf27ed7867b7 89 * @param *message buffer used to get SMS message
irsanjul 0:bf27ed7867b7 90 * @param index which SMS message to read
irsanjul 0:bf27ed7867b7 91 * @returns
irsanjul 0:bf27ed7867b7 92 * 0 on success,
irsanjul 0:bf27ed7867b7 93 * -1 on error
irsanjul 0:bf27ed7867b7 94 */
irsanjul 0:bf27ed7867b7 95 int readSMS(char *message, int index);
irsanjul 0:bf27ed7867b7 96
irsanjul 0:bf27ed7867b7 97 /** Delete SMS message on SIM card
irsanjul 0:bf27ed7867b7 98 * @param *index the index number which SMS message will be delete
irsanjul 0:bf27ed7867b7 99 * @returns
irsanjul 0:bf27ed7867b7 100 * 0 on success,
irsanjul 0:bf27ed7867b7 101 * -1 on error
irsanjul 0:bf27ed7867b7 102 */
irsanjul 0:bf27ed7867b7 103 int deleteSMS(int index);
irsanjul 0:bf27ed7867b7 104
irsanjul 0:bf27ed7867b7 105 /** Read SMS when coming a message,it will be store in messageBuffer.
irsanjul 0:bf27ed7867b7 106 * @param message buffer used to get SMS message
irsanjul 0:bf27ed7867b7 107 */
irsanjul 0:bf27ed7867b7 108 int getSMS(char* message);
irsanjul 0:bf27ed7867b7 109
irsanjul 0:bf27ed7867b7 110 /** Call someone
irsanjul 0:bf27ed7867b7 111 * @param *number the phone number which you want to call
irsanjul 0:bf27ed7867b7 112 * @returns
irsanjul 0:bf27ed7867b7 113 * 0 on success,
irsanjul 0:bf27ed7867b7 114 * -1 on error
irsanjul 0:bf27ed7867b7 115 */
irsanjul 0:bf27ed7867b7 116 int callUp(char *number);
irsanjul 0:bf27ed7867b7 117
irsanjul 0:bf27ed7867b7 118 /** Auto answer if coming a call
irsanjul 0:bf27ed7867b7 119 * @returns
irsanjul 0:bf27ed7867b7 120 * 0 on success,
irsanjul 0:bf27ed7867b7 121 * -1 on error
irsanjul 0:bf27ed7867b7 122 */
irsanjul 0:bf27ed7867b7 123 int answer(void);
irsanjul 0:bf27ed7867b7 124
irsanjul 0:bf27ed7867b7 125 /** Join GSM network
irsanjul 0:bf27ed7867b7 126 * @param *apn Access Point Name to connect network
irsanjul 0:bf27ed7867b7 127 * @param *userName general is empty
irsanjul 0:bf27ed7867b7 128 * @param *passWord general is empty
irsanjul 0:bf27ed7867b7 129 */
irsanjul 0:bf27ed7867b7 130 int join(char* apn, char* userName = NULL, char* passWord = NULL);
irsanjul 0:bf27ed7867b7 131
irsanjul 0:bf27ed7867b7 132 /** Disconnect from network
irsanjul 0:bf27ed7867b7 133 * @returns
irsanjul 0:bf27ed7867b7 134 * 0 on success,
irsanjul 0:bf27ed7867b7 135 * -1 on error
irsanjul 0:bf27ed7867b7 136 */
irsanjul 0:bf27ed7867b7 137 int disconnect(void);
irsanjul 0:bf27ed7867b7 138
irsanjul 0:bf27ed7867b7 139 /** Set blocking of the connection
irsanjul 0:bf27ed7867b7 140 * @param netopen_to time out of open the socket network in second
irsanjul 0:bf27ed7867b7 141 * @param cipopen_to time out of open the connection to server in second
irsanjul 0:bf27ed7867b7 142 * @param cipsend_to time out of send data to server in second
irsanjul 0:bf27ed7867b7 143 * @returns
irsanjul 0:bf27ed7867b7 144 * 0 on success,
irsanjul 0:bf27ed7867b7 145 * -1 on error
irsanjul 0:bf27ed7867b7 146 */
irsanjul 0:bf27ed7867b7 147 int SetBlocking(int netopen_to=5, int cipopen_to=5, int cipsend_to=5);
irsanjul 0:bf27ed7867b7 148
irsanjul 0:bf27ed7867b7 149 /** Build TCP connect
irsanjul 0:bf27ed7867b7 150 * @param *ip ip address which will connect to
irsanjul 0:bf27ed7867b7 151 * @param *port TCP server' port number
irsanjul 0:bf27ed7867b7 152 * @returns
irsanjul 0:bf27ed7867b7 153 * 0 on success,
irsanjul 0:bf27ed7867b7 154 * -1 on error
irsanjul 0:bf27ed7867b7 155 */
irsanjul 0:bf27ed7867b7 156 int connectTCP(char *ip, int port);
irsanjul 0:bf27ed7867b7 157
irsanjul 0:bf27ed7867b7 158 /** Send data to TCP server
irsanjul 0:bf27ed7867b7 159 * @param *data data that will be send to TCP server
irsanjul 0:bf27ed7867b7 160 * @returns
irsanjul 0:bf27ed7867b7 161 * 0 on success,
irsanjul 0:bf27ed7867b7 162 * -1 on error
irsanjul 0:bf27ed7867b7 163 */
irsanjul 0:bf27ed7867b7 164 int sendTCPData(char *data, int len);
irsanjul 0:bf27ed7867b7 165
irsanjul 0:bf27ed7867b7 166 /** Send data to TCP server
irsanjul 0:bf27ed7867b7 167 * @param *buff data that will be received from TCP server
irsanjul 0:bf27ed7867b7 168 * @param len size of buffer to read
irsanjul 0:bf27ed7867b7 169 * @returns
irsanjul 0:bf27ed7867b7 170 * 0 on success,
irsanjul 0:bf27ed7867b7 171 * -1 on error
irsanjul 0:bf27ed7867b7 172 */
irsanjul 0:bf27ed7867b7 173 int receivedTCPData(char *buff, int len);
irsanjul 0:bf27ed7867b7 174
irsanjul 0:bf27ed7867b7 175 /** Close TCP connection
irsanjul 0:bf27ed7867b7 176 * @returns
irsanjul 0:bf27ed7867b7 177 * 0 on success,
irsanjul 0:bf27ed7867b7 178 * -1 on error
irsanjul 0:bf27ed7867b7 179 */
irsanjul 0:bf27ed7867b7 180 int closeTCP(void);
irsanjul 0:bf27ed7867b7 181
irsanjul 0:bf27ed7867b7 182 /** Clear serial pipe
irsanjul 0:bf27ed7867b7 183 */
irsanjul 0:bf27ed7867b7 184 void purge(void);
irsanjul 0:bf27ed7867b7 185
irsanjul 0:bf27ed7867b7 186 Serial gprsSerial;
irsanjul 0:bf27ed7867b7 187 //USBSerial pc;
irsanjul 0:bf27ed7867b7 188
irsanjul 0:bf27ed7867b7 189 private:
irsanjul 0:bf27ed7867b7 190 /** Read from GSM module and save to buffer array
irsanjul 0:bf27ed7867b7 191 * @param *buffer buffer array to save what read from GSM module
irsanjul 0:bf27ed7867b7 192 * @param *count the maximal bytes number read from GSM module
irsanjul 0:bf27ed7867b7 193 * @returns
irsanjul 0:bf27ed7867b7 194 * 0 on success,
irsanjul 0:bf27ed7867b7 195 * -1 on error
irsanjul 0:bf27ed7867b7 196 */
irsanjul 0:bf27ed7867b7 197 int readBuffer(char *buffer,int count);
irsanjul 0:bf27ed7867b7 198
irsanjul 0:bf27ed7867b7 199 /** Send AT command to GSM module
irsanjul 0:bf27ed7867b7 200 * @param *cmd command array which will be send to GSM module
irsanjul 0:bf27ed7867b7 201 */
irsanjul 0:bf27ed7867b7 202 void sendCmd(char *cmd);
irsanjul 0:bf27ed7867b7 203
irsanjul 0:bf27ed7867b7 204 /** Check GSM module response before timeout
irsanjul 0:bf27ed7867b7 205 * @param *resp correct response which GSM module will return
irsanjul 0:bf27ed7867b7 206 * @param *timeout waiting seconds till timeout
irsanjul 0:bf27ed7867b7 207 * @returns
irsanjul 0:bf27ed7867b7 208 * 0 on success,
irsanjul 0:bf27ed7867b7 209 * -1 on error
irsanjul 0:bf27ed7867b7 210 */
irsanjul 0:bf27ed7867b7 211 int waitForResp(char *resp, int timeout);
irsanjul 0:bf27ed7867b7 212
irsanjul 0:bf27ed7867b7 213 /** Send AT command to GSM module and wait for correct response
irsanjul 0:bf27ed7867b7 214 * @param *cmd AT command which will be send to GSM module
irsanjul 0:bf27ed7867b7 215 * @param *resp correct response which GSM module will return
irsanjul 0:bf27ed7867b7 216 * @param *timeout waiting seconds till timeout
irsanjul 0:bf27ed7867b7 217 * @returns
irsanjul 0:bf27ed7867b7 218 * 0 on success,
irsanjul 0:bf27ed7867b7 219 * -1 on error
irsanjul 0:bf27ed7867b7 220 */
irsanjul 0:bf27ed7867b7 221 int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout);
irsanjul 0:bf27ed7867b7 222
irsanjul 0:bf27ed7867b7 223 Timer timeCnt;
irsanjul 0:bf27ed7867b7 224 char *phoneNumber;
irsanjul 0:bf27ed7867b7 225 char messageBuffer[SMS_MAX_LENGTH];
irsanjul 0:bf27ed7867b7 226 };
irsanjul 0:bf27ed7867b7 227
irsanjul 0:bf27ed7867b7 228 #endif // _GSM_H_
irsanjul 0:bf27ed7867b7 229