comparison gsm-fw/services/etm/etm_at.c @ 164:d78219c43fbf

gsm-fw/services/etm: initial import from the Leonardo semi-src
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 18 Nov 2013 06:39:44 +0000
parents
children
comparison
equal deleted inserted replaced
163:5e0e41cd7c9f 164:d78219c43fbf
1 /********************************************************************************
2 * Enhanced TestMode (ETM)
3 * @file etm_at.c (Support for AT-commands)
4 *
5 * @author Kim T. Peteren (ktp@ti.com)
6 * @version 0.2
7 *
8
9
10 *
11 * History:
12 *
13 * Date Modification
14 * ------------------------------------
15 * 16/06/2003 Creation
16 * 06/11/2003 Small updates
17 * 18/02/2004 Major updating, the event handling has been updated
18 *
19 *
20 * (C) Copyright 2003 by Texas Instruments Incorporated, All Rights Reserved
21 *********************************************************************************/
22
23 //#include "aci_msg.h"
24
25 #include "etm/etm.h"
26 #include "etm/etm_api.h"
27 #include "etm/etm_at.h"
28 #include "etm/etm_trace.h"
29
30 #include "rv/rv_general.h"
31 #include "rvf/rvf_api.h"
32
33 #include "atp/atp_env.h" // FixMe
34 #include "atp/atp_i.h" // FixMe
35 //#include "atp/atp_general.h" // FixMe
36
37 #include "atp/atp_api.h"
38 #include "atp/atp_messages.h"
39
40 #include <string.h>
41
42 // Defined in aci_msg.h
43 extern T_XAT_SYNC_MESSAGE;
44 extern SYNC_PORT_NUM;
45 extern ASYNC_PORT_NUM;
46 extern T_RAT_CALLBACK_MESSAGE;
47
48
49 /******************************************************************************
50 * Globals
51 *****************************************************************************/
52
53 static T_ATP_SW_ENTITY_NAME ETM_AT_ADAPTER = "ETMATA"; // max 5 caracter
54 static T_ATP_SW_ENTITY_ID etm_at_id;
55 static T_ATP_PORT_NB etm_at_to_aaa_port = 0x01; // equal to SYNC_PORT_NUM
56
57 static T_ATP_SW_ENTITY_NAME aaa_name = "AAA"; // ACIA ADAPTER
58 //static T_ATP_SW_ENTITY_NAME aaa_name = "GSM"; // GSM ADAPTER
59 static T_ATP_SW_ENTITY_ID aaa_entity_id;
60 static T_ATP_ENTITY_MODE aaa_mode;
61
62 static T_ATP_CALLBACK etm_at_return_path;
63 static T_ATP_ENTITY_MODE etm_at_mode;
64
65 //extern T_ETM_ENV_CTRL_BLK *etm_env_ctrl_blk;
66
67 static int etm_at_initialized = 0;
68 static int etm_at_event_status = 0;
69
70 static char etm_at_latest_cmd[9] = { 0 };
71
72
73 /******************************************************************************
74 * Internal prototypes
75 *****************************************************************************/
76
77 int etm_at_init(void);
78 int etm_at_reg_req(void);
79 int etm_at_open_port_req(void);
80 //T_RV_HDR *etm_at_wait_for_atp_event (UINT16 event_code);
81 int etm_at_atp_txt_cmd_rdy( T_ATP_TXT_CMD_RDY *msg);
82 void etm_at_callback_for_ATP(void *event_from_atp_p);
83
84
85 /******************************************************************************
86 * AT commands List
87 *****************************************************************************/
88
89 struct at_async_trans_s {
90 // const short index; // index for ...
91 const char *name; // parameter
92 };
93
94 static struct at_async_trans_s at_cmd[] =
95 {
96 { "TEST" },
97 { "atd" },
98 { NULL }
99 };
100
101
102 int at_cmd_search(char *at_string)
103 {
104 struct at_async_trans_s *at_p = at_cmd;
105 int size, error;
106
107 size = strlen(at_string);
108 tr_etm(TgTrCore, "ETM CORE: _cmd_search: at_string size(%d)", size);
109
110 if (size > 8)
111 strncpy(&etm_at_latest_cmd[0], at_string, 8);
112 else
113 strncpy(&etm_at_latest_cmd[0], at_string, size);
114
115 // if ((etm_at_latest_cmd[2]== '+') || (etm_at_latest_cmd[2] == '%'))
116 // return ETM_OK;
117
118 while (at_p->name) {
119 error = strncmp(&etm_at_latest_cmd[0], at_p->name, strlen(at_p->name));
120 if (error == 0)
121 strcpy(&etm_at_latest_cmd[0], at_p->name);
122 tr_etm(TgTrCore, "ETM CORE: _cmd_search: AT list(%s)", at_p->name);
123 at_p++;
124 }
125
126 tr_etm(TgTrCore, "ETM CORE: _cmd_search: text(%s)", &etm_at_latest_cmd[0]);
127
128 return ETM_OK;
129 }
130
131
132 /******************************************************************************
133 * AT command to ACI
134 *****************************************************************************/
135
136 int etm_at_adapter(char *command)
137 {
138 int error;
139 T_ATP_TXT_CMD txt_cmd_p = NULL;
140
141 if (!etm_at_initialized){
142 if ((etm_at_init() == ETM_OK) && (etm_at_event_status == ETM_OK)) {
143 // read etm_at_event_status
144 tr_etm(TgTrCore, "ETM CORE: _at_adapter: initialization - OK");
145 etm_at_initialized = 1;
146 }
147 else {
148 tr_etm(TgTrCore, "ETM CORE: _at_adapter: initialization - FAILED");
149 return ETM_FATAL;
150 }
151 }
152
153 // Creating ETM_AT data buffer, will be fread by atp_send_txt_cmd()
154 if ((txt_cmd_p = etm_malloc(strlen(command)+1)) == NULL)
155 return ETM_NOMEM;
156
157 strcpy(txt_cmd_p, command);
158
159 // Find AT command
160 //at_cmd_search(command);
161
162 // Send AT command to AAA
163 if ((error = atp_send_txt_cmd(etm_at_id, etm_at_to_aaa_port, AT_CMD, txt_cmd_p)) != RV_OK) {
164 tr_etm(TgTrCore, "ETM CORE: _at_adapter: send_txt_cmd - FAILED");
165 return ETM_FATAL;
166 }
167
168 return ETM_OK;
169 }
170
171
172 /******************************************************************************
173 * ETM AT Adapter Initialization
174 *****************************************************************************/
175
176 int etm_at_init(void)
177 {
178 int error;
179
180 // Check if ATP has been started if NOT
181 // Turn ATP module ON - necessary for RVM
182 if (atp_swe_state != ATP_STARTED)
183 if (atp_start() != RV_OK)
184 return ETM_FATAL;
185
186
187 // Registration of ETM_AT to ATP
188 if ((error = etm_at_reg_req()) != ETM_OK) {
189 tr_etm(TgTrCore, "ETM CORE: _at_init: Registration ERROR(%d)", error);
190 return error;
191 }
192
193 // Open a port to ACI adapter
194 if ((error = etm_at_open_port_req()) != ETM_OK) {
195 tr_etm(TgTrCore, "ETM CORE: _at_init: Open port ERROR(%d)", error);
196 return error;
197 }
198
199 return ETM_OK;
200 }
201
202
203 // Register of ETM AT adapter with ATP.
204
205 int etm_at_reg_req(void)
206 {
207 int result;
208
209 // Registration of ETM_AT in ATP
210 etm_at_return_path.addr_id = NULL; // mailbox identifier - unused when callback mechanism in use
211 etm_at_return_path.callback_func = etm_at_callback_for_ATP; // Pointer to callback fn ...
212
213 // Set modes supported by SWE
214 etm_at_mode.cmd_mode = TXT_MODE; // INTERPRETED_MODE/TXT_MODE
215 etm_at_mode.cp_mode = COPY_OFF;
216 etm_at_mode.cmd_support_mode = CMD_SUPPORT_ON;
217
218 // Registration of ETM_AT to ATP
219 if ((result = (atp_reg(ETM_AT_ADAPTER, etm_at_return_path, etm_at_mode, &etm_at_id)))
220 != RV_OK) {
221 tr_etm(TgTrCore, "ETM CORE: _at_reg_req: ERROR(%d)", result);
222 return ETM_RV_FATAL;
223 }
224
225 // Check ETM_AT Registration
226 if ((result = (atp_reg_info(ETM_AT_ADAPTER, &etm_at_id, &etm_at_mode)))
227 != RV_OK){
228 tr_etm(TgTrCore, "ETM CORE: _at_reg_req: FAILED");
229 return ETM_RV_NOT_SUPPORTED;
230 }
231
232 return ETM_OK;
233 }
234
235
236 // Open a port with ATP.
237
238 int etm_at_open_port_req(void)
239 {
240 int result;
241 T_ATP_NO_COPY_INFO etm_no_copy_info;
242 T_ATP_PORT_INFO etm_port_info;
243 T_ATP_CUSTOM_INFO *cust_info_p = NULL;
244 // T_RV_HDR *message_p;
245 T_RVF_MB_ID etm_mb_id;
246
247 if (rvf_get_mb_id("ETM_PRIM", &etm_mb_id) != RVF_OK) {
248 tr_etm(TgTrCore, "ETM CORE: _at_open_port_req: Memory bank ETM does not exist!");
249 return ETM_RV_FATAL;
250 }
251
252 /* Test header and trailer removal from ATP so: trailers and headers equal 0 */
253 etm_no_copy_info.tx_mb = etm_mb_id; /* MB used by ATP is from ETM */
254 etm_no_copy_info.rx_mb = etm_mb_id; /* MB used by ATP is from ETM */
255 etm_no_copy_info.rx_head_mode = RX_HEADER_OFF;
256 etm_no_copy_info.rx_head_size = 0x00;
257 etm_no_copy_info.rx_trail_size = 0x00;
258 etm_no_copy_info.tx_head_mode = TX_HEADER_OFF;
259 etm_no_copy_info.tx_head_size = 0x00;
260 etm_no_copy_info.tx_trail_size = 0x00;
261 etm_no_copy_info.packet_mode = NORMAL_PACKET; /* No L2CAP packet... */
262
263 // Port info
264 etm_port_info.port_config = NOT_DEFINED_CONFIG;
265 etm_port_info.ring_type = ATP_NO_RING_TYPE;
266 etm_port_info.signal_mask = (T_ATP_SIGNAL_MASK) ATP_ALL_THE_SIGNAL_UNMASK; /* Get all signal changed event */
267 etm_port_info.dce_mask[0] = 0x0000; /* No requirement in term of DCE */
268
269 // Test AA Adapter Registration
270 if (atp_reg_info(aaa_name, &aaa_entity_id, &aaa_mode) != RV_OK) {
271 tr_etm(TgTrCore, "ETM CORE: _at_open_port_req: AAA is not registered to ATP");
272 return ETM_RV_NOT_SUPPORTED;
273 }
274
275 // Open a virtual port between ETM AT adapter and ACI adapter
276 if ((result = atp_open_port_rqst(etm_at_id, aaa_entity_id, etm_at_to_aaa_port,
277 etm_port_info, etm_no_copy_info, cust_info_p)) != RV_OK) {
278 tr_etm(TgTrCore, "ETM CORE: _at_open_port_req: FAILED");
279 return ETM_RV_FATAL;
280 }
281
282 // etm_at_callback_for_ATP should receive event: ATP_OPEN_PORT_CFM
283 rvf_wait(0xffff, 100); // Timeout 100 ticks
284 tr_etm(TgTrCore, "ETM CORE: _at_open_port_req: STATUS(%d)", etm_at_event_status);
285
286 return ETM_OK;
287 }
288
289
290 /******************************************************************************
291 * Close Port
292 *****************************************************************************/
293
294 int etm_at_port_close_req(void)
295 {
296 int error;
297
298 error = atp_close_port(etm_at_id, etm_at_to_aaa_port);
299 if (error != RV_OK) {
300 tr_etm(TgTrCore, "ETM CORE: _at_port_close_req: FAILED");
301 return ETM_FATAL;
302 }
303
304 // etm_at_callback_for_ATP should receive event: ATP_PORT_CLOSED
305 rvf_wait(0xffff, 100); // Timeout 100 ticks
306 tr_etm(TgTrCore, "ETM CORE: _at_open_port_req: STATUS(%d)", etm_at_event_status);
307
308 etm_at_initialized = 0;
309 return ETM_OK;
310 }
311
312
313 /******************************************************************************
314 * Callback function for ATP
315 *****************************************************************************/
316
317 // PURPOSE : Decipher and route incoming messages from ATP.
318
319 void etm_at_callback_for_ATP(void *event_from_atp_p)
320 {
321 // This function is ATP context.
322
323 // tr_etm(TgTrEtmLow,"ETM: CORE: etm_at_callback_for_ATP: recv. event (0x%x)", ((T_RV_HDR *) event_from_atp_p)->msg_id);
324
325 // What type of event?
326 switch (((T_RV_HDR *) event_from_atp_p)->msg_id)
327 {
328 case ATP_CMD_RDY:
329 tr_etm(TgTrCore,"ETM CORE: _at_callback_for_AT: UNSUPPORTED");
330 break;
331 case ATP_OPEN_PORT_CFM:
332 if (((T_ATP_OPEN_PORT_CFM *) event_from_atp_p)->result == OPEN_PORT_OK) {
333 tr_etm(TgTrCore, "ETM CORE: _at_callback_for_ATP: rev. event ATP_OPEN_PORT_CFM - OPEN_PORT_OK");
334 // tr_etm(TgTrCore, "ETM CORE: _at_callback_for_ATP: rev. event ATP_OPEN_PORT_CFM - Port Number (%d)",
335 // ((T_ATP_OPEN_PORT_CFM *) event_from_atp_p)->initiator_port_nb);
336 }
337 else {
338 tr_etm(TgTrCore, "ETM CORE: _at_callback_for_ATP: rev. event ATP_OPEN_PORT_CFM - OPEN_PORT_NOK");
339 etm_at_event_status = ETM_FATAL;
340 }
341 // tr_etm(TgTrCore, "ETM CORE: _at_callback_for_ATP: rev. event ATP_OPEN_PORT_CFM - Port Number (%d)",
342 // ((T_ATP_OPEN_PORT_CFM *) event_from_atp_p)->initiator_port_nb);
343 break;
344 case ATP_TXT_CMD_RDY:
345 etm_at_atp_txt_cmd_rdy((T_ATP_TXT_CMD_RDY *) event_from_atp_p);
346 break;
347 case ATP_PORT_CLOSED:
348 tr_etm(TgTrCore, "ETM CORE: _at_callback_for_ATP: rev. event ATP_PORT_CLOSED");
349 break;
350 case ATP_OPEN_PORT_IND:
351 tr_etm(TgTrCore, "ETM CORE: _at_callback_for_ATP: rev. event ATP_OPEN_PORT_IND");
352 break;
353 default:
354 tr_etm(TgTrCore, "ETM CORE: _at_callback_for_ATP: rev. unknown event(0x%x)- UNSUPPORTED",
355 ((T_RV_HDR *) event_from_atp_p)->msg_id);
356 }
357
358 /* Free memmory that is allocated within ATP */
359 etm_free(event_from_atp_p);
360 }
361
362
363 // This is called when the result of the AT cmd is received
364 // (in a primetive) from the ATP entity.
365 int etm_at_atp_txt_cmd_rdy(T_ATP_TXT_CMD_RDY *msg)
366 {
367 /* Send reply to PC
368 The status type depend of the event from ATP module:
369 last_result = 0, means more data is sent to HOST (PC)
370 last_result = 1, means last data is sent to HOST (PC)
371 last_result = 2, means data is not sent to HOST (PC) */
372
373 T_ETM_PKT *pkt;
374 char *text, last_result = 1;
375 int error = 0, length;
376
377 if ((pkt = (T_ETM_PKT *) etm_malloc(sizeof(T_ETM_PKT))) == NULL) {
378 rvf_dump_mem();
379 return ETM_NOMEM;
380 }
381
382 // Init. of return packet
383 pkt->mid = ETM_CORE;
384 pkt->status = ETM_OK;
385 pkt->size = 0;
386 pkt->index = 0;
387 etm_pkt_put8(pkt, 'G'); // 'G' is indcator for AT command respons
388
389
390 tr_etm(TgTrCore, "ETM CORE: _at_atp_txt_cmd_rdy: ATP_TXT_CMD_RDY with cmd_type(%d)",
391 msg->cmd_type);
392
393 switch (msg->cmd_type){
394 case AT_CMD: // Type: 0
395 case CUSTOM_CMD: // Type: 4
396 case CMD_ABORT: // Type: 5
397 case UNKNOWN: error = ETM_MESSAGE; break; // Type: 6
398 case PRELIMINARY_RESULT_CODE: last_result = 0; break; // Type: 7
399 // case INFORMATION_TXT: last_result = 0; break; // Type: 3
400 case RESULT_CODE: last_result = 1; break; // Type: 1
401 case UNSOLICITED_RESULT: last_result = 2; break; // Type: 2
402 default:
403 tr_etm(TgTrCore,"ETM CORE: _at_atp_txt_cmd_rdy: cmd_tpye(%d) - FAILED", msg->cmd_type);
404 error = ETM_NOSYS;
405 }
406
407 if (last_result == 2)
408 goto etm_at_atp_txt_cmd_rdy_end;
409
410 text = ((char *) msg->txt_cmd_p);
411 length = strlen(text);
412 etm_pkt_putdata(pkt, text, length);
413 tr_etm(TgTrCore, "ETM CORE: _at_atp_txt_cmd_rdy: text(%s) length(%d)", text, length);
414
415 // Status will be set to ETM_OK_MORE when more data is send.
416 // Add one because of string length is also returned as part of the result
417 pkt->status = (last_result ? ETM_OK : -ETM_OK_MORE);
418
419 if (error < 0) {
420 // tr_etm(TgTrCore,"ETM CORE: _at_atp_txt_cmd_rdy: ERROR(%d)", error);
421 pkt->status = -error;
422 }
423
424 etm_pkt_send(pkt);
425
426 etm_at_atp_txt_cmd_rdy_end:
427 etm_free(pkt);
428
429 return ETM_OK;
430 }
431
432
433 /******************************************************************************
434 * ETM AT - Main Task
435 *****************************************************************************/
436
437 // Structur of protocol data dl-link: |target|at_cmd|
438 int etm_at(T_ETM_PKT *pkt, char *buf)
439 {
440 int error = ETM_NOSYS;
441 int sw_entity;
442
443 // sw_entity = *buf++;
444
445 // FIXME pkt should be use in etm_at_adapter()
446 error = etm_at_adapter((char *) buf);
447
448 #if 0
449 switch (sw_entity) {
450 case GSM:
451
452 break;
453 case BLUE:
454 //error = etm_at_blue(*buf++);
455 break;
456 default:
457 tr_etm(TgTrCore,"ETM CORE: _at: ERROR(%d)", error);
458 error = ETM_NOSYS;
459 }
460 #endif
461
462 return error;
463 }
464