-
Notifications
You must be signed in to change notification settings - Fork 0
/
telit_socket.c
288 lines (244 loc) · 6.65 KB
/
telit_socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* telit_socket.c
*
* Created on: 13 Aug 2024
* Author: jorda
*
* Rutronik Elektronische Bauelemente GmbH Disclaimer: The evaluation board
* including the software is for testing purposes only and,
* because it has limited functions and limited resilience, is not suitable
* for permanent use under real conditions. If the evaluation board is
* nevertheless used under real conditions, this is done at one’s responsibility;
* any liability of Rutronik is insofar excluded
*/
#include "telit_socket.h"
#include "rab_rtk.h"
#include <stdio.h>
#include "cy_syslib.h"
telit_socket_status_e local_status = TELIT_SOCKET_STATUS_CHECK_MODEM_STATUS;
static int is_modem_on()
{
telit_pin_status_e pin_status = PIN_STATUS_UNKNOWN;
int retval = rab_rtk_telit_get_pin_status(&pin_status);
if (retval != 0)
{
// Not on, MODEM does not react
return 0;
}
// Modem is ready
return 1;
}
static int init_internet_connection()
{
int retval = rab_rtk_telit_enable_disable_error_codes(2);
if (retval != 0)
{
printf("rab_rtk_telit_enable_disable_error_codes error: %d\r\n", retval);
return -1;
}
telit_pin_status_e pin_status = PIN_STATUS_UNKNOWN;
retval = rab_rtk_telit_get_pin_status(&pin_status);
if (retval != 0)
{
printf("rab_rtk_telit_get_pin_status error: %d\r\n", retval);
return -2;
}
printf("PIN status: %d \r\n", pin_status);
if (pin_status != PIN_STATUS_READY)
{
printf("PIN not ready, stop\r\n");
return -3;
}
// Need to wait for the attached operator (it might take time....)
telit_access_technology_e access_technology = ACCESS_TECHNOLOGY_UNKNOWN;
for(int i = 0; i < 20; ++i)
{
// Get the attached operator, if not attached, exit
// it will also gives the network type (GSM or other)
retval = rab_rtk_telit_get_attached_operator(&access_technology);
if (retval != 0)
{
CyDelay(1000);
}
else break;
}
if (access_technology == ACCESS_TECHNOLOGY_UNKNOWN)
{
printf("Error unknown technology\r\n");
return -4;
}
printf("Access technology: %d \r\n", access_technology);
for(int i = 0; i < 10; ++i)
{
telit_registration_status_e registration_status = REGISTRATION_STATUS_UNKNOWN;
retval = rab_rtk_telit_get_registration_status(®istration_status);
if (retval != 0)
{
printf("rab_rtk_telit_get_registration_status error: %d\r\n", retval);
return -5;
}
if (registration_status == REGISTRATION_STATUS_REGISTERED)
{
printf("Registration status: REGISTRATION_STATUS_REGISTERED\r\n");
break;
}
else if (registration_status == REGISTRATION_STATUS_ROAMING)
{
printf("Registration status: REGISTRATION_STATUS_ROAMING\r\n");
break;
}
else
{
printf("Not registered to network, stop\r\n");
if (i == 9) return -6;
CyDelay(1000);
}
}
pdp_context_status_result_t pdp_context_status;
retval = rab_rtk_telit_get_pdp_context_status(&pdp_context_status);
if (retval != 0)
{
printf("rab_rtk_telit_get_pdp_context_status error: %d\r\n", retval);
return -7;
}
if (pdp_context_status.status == 1)
{
// if already activated, deactivate it (because else some strange behavior when opening a socket
printf("PDP activated -> Deactivate it \r\n");
rab_rtk_telit_activate_deactivate_pdp_context(1, 0);
return -8;
}
retval = rab_rtk_telit_get_pdp_context_status(&pdp_context_status);
if (retval != 0)
{
printf("rab_rtk_telit_get_pdp_context_status error: %d\r\n", retval);
return -7;
}
// Define PDP context and activate PDP context
retval = rab_rtk_telit_define_pdp_context();
if (retval != 0)
{
printf("rab_rtk_telit_define_pdp_context error: %d\r\n", retval);
return -9;
}
retval = rab_rtk_telit_activate_deactivate_pdp_context(1, 1);
if (retval != 0)
{
printf("rab_rtk_telit_activate_deactivate_pdp_context error: %d\r\n", retval);
return -10;
}
return 0;
}
int telit_socket_init()
{
int retval = rab_rtk_telit_init_hardware();
if (retval != 0) return retval;
return 0;
}
telit_socket_status_e telit_socket_do()
{
switch(local_status)
{
case TELIT_SOCKET_STATUS_CHECK_MODEM_STATUS:
// TODO: always reboot for safe start -> then wait for 30 seconds
rab_rtk_telit_power_off();
rab_rtk_telit_power_on();
printf("Wait for 30 seconds\r\n");
for(int i = 0; i < 30; ++i)
{
CyDelay(1000);
}
printf("Let's go\r\n");
local_status = TELIT_SOCKET_STATUS_WAIT_MODEM_ON;
// if (is_modem_on())
// {
// // Good -> init context
// printf("Modem is ON at start\r\n");
// local_status = TELIT_SOCKET_STATUS_INIT_INTERNET_CONNECTION;
// }
// else
// {
// printf("Modem is OFF at start\r\n");
// // Start (duration 2.6 seconds - blocking) and wait
// rab_rtk_telit_power_on();
// local_status = TELIT_SOCKET_STATUS_WAIT_MODEM_ON;
// }
break;
case TELIT_SOCKET_STATUS_WAIT_MODEM_ON:
if (is_modem_on())
{
printf("Modem is ON now\r\n");
local_status = TELIT_SOCKET_STATUS_INIT_INTERNET_CONNECTION;
}
break;
case TELIT_SOCKET_STATUS_INIT_INTERNET_CONNECTION:
{
int retval = init_internet_connection();
if (retval != 0)
{
printf("TELIT_SOCKET_STATUS_ERROR\r\n");
local_status = TELIT_SOCKET_STATUS_ERROR;
}
else
{
printf("TELIT_SOCKET_STATUS_CONNECTED\r\n");
local_status = TELIT_SOCKET_STATUS_CONNECTED;
}
break;
}
case TELIT_SOCKET_STATUS_CONNECTED:
break;
case TELIT_SOCKET_STATUS_ERROR:
break;
}
return local_status;
}
int telit_socket_open(char* address, uint16_t port)
{
// Try to open a socket with the server
int retval = rab_rtk_telit_open_socket(address, port);
if (retval != 0)
{
printf("rab_rtk_telit_open_socket error: %d\r\n", retval);
return -1;
}
// Wait until connection state is Okay
telit_raw_socket_status_e socket_status = SOCKET_STATUS_CLOSED;
for(int i = 0; i < 5; ++i)
{
rab_rtk_telit_get_socket_status(&socket_status);
if (socket_status != SOCKET_STATUS_CLOSED) break;
CyDelay(500);
}
if (socket_status == SOCKET_STATUS_CLOSED)
{
printf("Connection closed - close socket and exit. \r\n");
rab_rtk_telit_close_socket();
rab_rtk_telit_activate_deactivate_pdp_context(1, 0);
return -2;
}
return 0;
}
void telit_socket_close()
{
rab_rtk_telit_close_socket();
rab_rtk_telit_activate_deactivate_pdp_context(1, 0);
}
int telit_socket_write(uint16_t buffer_len, uint8_t* buffer)
{
return rab_rtk_telit_socket_write(1, buffer_len, buffer);
}
int telit_socket_read(uint16_t buffer_len, uint8_t* buffer)
{
return rab_rtk_telit_socket_read(1, buffer_len, buffer);
}
int telit_socket_get_buffer_in_size(uint16_t* len)
{
uint16_t sent_size = 0;
uint16_t received_size = 0;
uint16_t buff_in_size = 0;
int retval = rab_rtk_telit_get_socket_information(&sent_size, &received_size, &buff_in_size);
if (retval != 0) return -1;
*len = buff_in_size;
return 0;
}