f36b482ec2544bd5cd903a6366079dd68b30e86a
[framework/telephony/libtcore.git] / src / co_sms.c
1 /*
2  * libtcore
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <glib.h>
26
27 #include "tcore.h"
28 #include "plugin.h"
29 #include "user_request.h"
30 #include "co_sms.h"
31
32 struct private_object_data {
33         struct tcore_sms_operations *ops;
34         gboolean b_readyStatus;
35 };
36
37 /**
38  * This function is used to encode SMS Parameters to TPDU on EFsmsp
39  *
40  * @return              length of string
41  * @param[in]           incoming - telephony_sms_Params_t
42  * @param[in]           data - TPDU data
43  * @Interface           Synchronous.
44  * @remark
45  * @Refer
46  */
47 int _tcore_util_sms_encode_smsParameters(const struct telephony_sms_Params *incoming, unsigned char *data, int SMSPRecordLen)
48 {
49         struct telephony_sms_Params *smsParams =  (struct telephony_sms_Params *)incoming;
50         unsigned int nPIDIndex = 0;
51         unsigned char nOffset = 0;
52
53         if(incoming == NULL || data == NULL)
54                 return FALSE;
55
56         memset(data, 0xff, SMSPRecordLen);//pSmsParam->RecordLen);
57
58         dbg(" Record index = %d", (int) smsParams->recordIndex);
59         dbg(" Alpha ID Len = %d", (int) smsParams->alphaIdLen);
60         dbg(" Record Length : %d", SMSPRecordLen);//pSmsParam->RecordLen);
61
62         if (SMSPRecordLen/*pSmsParam->RecordLen*/>= nDefaultSMSPWithoutAlphaId) {
63                 nPIDIndex = SMSPRecordLen
64                                 /*pSmsParam->RecordLen*/- nDefaultSMSPWithoutAlphaId;
65         }
66
67         //dongil01.park(2008/12/27) - Check Point
68         memcpy(data, smsParams->szAlphaId, (int) nPIDIndex/*pSmsParam->AlphaIdLen*/);
69
70         dbg(" Alpha ID : %s", smsParams->szAlphaId);
71         dbg(" nPIDIndex = %d", nPIDIndex);
72
73         data[nPIDIndex] = smsParams->paramIndicator;
74
75         dbg(" Param Indicator = %02x",  smsParams->paramIndicator);
76
77         if ((smsParams->paramIndicator & SMSPValidDestAddr) == 0x00) {
78                 nOffset = nDestAddrOffset;
79
80                 data[nPIDIndex + (nOffset)] = smsParams->tpDestAddr.dialNumLen + 1;
81                 data[nPIDIndex + (++nOffset)] = ((smsParams->tpDestAddr.typeOfNum << 4) | smsParams->tpDestAddr.numPlanId) | 0x80;
82
83                 memcpy(&data[nPIDIndex + (++nOffset)], &smsParams->tpDestAddr.diallingNum, smsParams->tpDestAddr.dialNumLen);
84
85                 dbg(" nextIndex = %d", nPIDIndex);
86         }
87
88         if( (smsParams->paramIndicator & SMSPValidSvcAddr) == 0x00 )
89         {
90                 dbg("TON [%d] / NPI [%d]", smsParams->tpSvcCntrAddr.typeOfNum, smsParams->tpSvcCntrAddr.numPlanId);
91
92                 nOffset = nSCAAddrOffset;
93
94                 dbg("SCA Length : %d", smsParams->tpSvcCntrAddr.dialNumLen);
95
96                 data[nPIDIndex + (nOffset)] = smsParams->tpSvcCntrAddr.dialNumLen + 1;
97                 data[nPIDIndex + (++nOffset)] = ((smsParams->tpSvcCntrAddr.typeOfNum << 4) | smsParams->tpSvcCntrAddr.numPlanId) | 0x80;
98
99                 memcpy(&data[nPIDIndex + (++nOffset)], &smsParams->tpSvcCntrAddr.diallingNum, smsParams->tpSvcCntrAddr.dialNumLen);
100
101                 dbg(" nextIndex = %d", nPIDIndex);
102         }
103
104         if ((smsParams->paramIndicator & SMSPValidPID) == 0x00) {
105                 nOffset = nPIDOffset;
106
107                 data[nPIDIndex + nOffset] = smsParams->tpProtocolId;
108                 dbg(" PID = %02x", smsParams->tpProtocolId);
109                 dbg(" nextIndex = %d", nPIDIndex);
110         }
111
112         if ((smsParams->paramIndicator & SMSPValidDCS) == 0x00) {
113                 nOffset = nDCSOffset;
114
115                 data[nPIDIndex + nOffset] = smsParams->tpDataCodingScheme;
116                 dbg(" DCS = %02x", smsParams->tpDataCodingScheme);
117                 dbg(" nextIndex = %d", nPIDIndex);
118         }
119
120         if ((smsParams->paramIndicator & SMSPValidVP) == 0x00) {
121                 nOffset = nVPOffset;
122
123                 data[nPIDIndex + nOffset] = smsParams->tpValidityPeriod;
124                 dbg(" VP = %02x", smsParams->tpValidityPeriod);
125                 dbg(" nextIndex = %d", nPIDIndex);
126         }
127
128         return TRUE;
129
130 }
131
132 static void _clone_sms_operations(struct private_object_data *po, struct tcore_sms_operations *sms_ops)
133 {
134         if(sms_ops->send_umts_msg) {
135                 po->ops->send_umts_msg = sms_ops->send_umts_msg;
136         }
137         if(sms_ops->read_msg) {
138                 po->ops->read_msg = sms_ops->read_msg;
139         }
140         if(sms_ops->save_msg) {
141                 po->ops->save_msg = sms_ops->save_msg;
142         }
143         if(sms_ops->delete_msg) {
144                 po->ops->delete_msg = sms_ops->delete_msg;
145         }
146         if(sms_ops->get_storedMsgCnt) {
147                 po->ops->get_storedMsgCnt = sms_ops->get_storedMsgCnt;
148         }
149         if(sms_ops->get_sca) {
150                 po->ops->get_sca = sms_ops->get_sca;
151         }
152         if(sms_ops->set_sca) {
153                 po->ops->set_sca = sms_ops->set_sca;
154         }
155         if(sms_ops->get_cb_config) {
156                 po->ops->get_cb_config = sms_ops->get_cb_config;
157         }
158         if(sms_ops->set_cb_config) {
159                 po->ops->set_cb_config = sms_ops->set_cb_config;
160         }
161         if(sms_ops->set_mem_status) {
162                 po->ops->set_mem_status = sms_ops->set_mem_status;
163         }
164         if(sms_ops->get_pref_brearer) {
165                 po->ops->get_pref_brearer = sms_ops->get_pref_brearer;
166         }
167         if(sms_ops->set_pref_brearer) {
168                 po->ops->set_pref_brearer = sms_ops->set_pref_brearer;
169         }
170         if(sms_ops->set_delivery_report) {
171                 po->ops->set_delivery_report = sms_ops->set_delivery_report;
172         }
173         if(sms_ops->set_msg_status) {
174                 po->ops->set_msg_status = sms_ops->set_msg_status;
175         }
176         if(sms_ops->get_sms_params) {
177                 po->ops->get_sms_params = sms_ops->get_sms_params;
178         }
179         if(sms_ops->set_sms_params) {
180                 po->ops->set_sms_params = sms_ops->set_sms_params;
181         }
182         if(sms_ops->get_paramcnt) {
183                 po->ops->get_paramcnt = sms_ops->get_paramcnt;
184         }
185         if(sms_ops->send_cdma_msg) {
186                 po->ops->send_cdma_msg = sms_ops->send_cdma_msg;
187         }
188
189         return;
190 }
191
192 static TReturn _dispatcher(CoreObject *o, UserRequest *ur)
193 {
194         enum tcore_request_command command;
195         struct private_object_data *po = NULL;
196         TReturn rtn = TCORE_RETURN_SUCCESS;
197
198         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_SMS, TCORE_RETURN_EINVAL);
199
200         po = tcore_object_ref_object(o);
201         if (!po || !po->ops) {
202                 dbg("[tcore_SMS] ERR: private_object is NULL or ops is NULL");
203                 return TCORE_RETURN_ENOSYS;
204         }
205
206         if(po->b_readyStatus == FALSE) {
207                 dbg("[tcore_SMS] DEVICE_NOT_READY");
208                 return TCORE_RETURN_ENOSYS; /* TAPI_API_NETTEXT_DEVICE_NOT_READY */
209         }
210
211         command = tcore_user_request_get_command(ur);
212         switch (command) {
213                 case TREQ_SMS_SEND_UMTS_MSG:
214                         if (!po->ops->send_umts_msg)
215                         {
216                                 dbg("[tcore_SMS] ERR: po->ops->send_umts_msg is NULL");
217                                 return TCORE_RETURN_ENOSYS;
218                         }
219
220                         rtn = po->ops->send_umts_msg(o, ur);
221
222                         break;
223                 case TREQ_SMS_READ_MSG:
224                         if (!po->ops->read_msg)
225                         {
226                                 dbg("[tcore_SMS] ERR: po->ops->read_msg is NULL");
227                                 return TCORE_RETURN_ENOSYS;
228                         }
229
230                         rtn = po->ops->read_msg(o, ur);
231
232                         break;
233                 case TREQ_SMS_SAVE_MSG:
234                         if (!po->ops->save_msg)
235                         {
236                                 dbg("[tcore_SMS] ERR: po->ops->save_msg is NULL");
237                                 return TCORE_RETURN_ENOSYS;
238                         }
239
240                         rtn = po->ops->save_msg(o, ur);
241
242                         break;
243                 case TREQ_SMS_DELETE_MSG:
244                         if (!po->ops->delete_msg)
245                         {
246                                 dbg("[tcore_SMS] ERR: po->ops->delete_msg is NULL");
247                                 return TCORE_RETURN_ENOSYS;
248                         }
249
250                         rtn = po->ops->delete_msg(o, ur);
251
252                         break;
253                 case TREQ_SMS_GET_COUNT:
254                         if (!po->ops->get_storedMsgCnt)
255                         {
256                                 dbg("[tcore_SMS] ERR: po->ops->get_storedMsgCnt is NULL");
257                                 return TCORE_RETURN_ENOSYS;
258                         }
259
260                         rtn = po->ops->get_storedMsgCnt(o, ur);
261
262                         break;
263                 case TREQ_SMS_GET_SCA:
264                         if (!po->ops->get_sca)
265                         {
266                                 dbg("[tcore_SMS] ERR: po->ops->get_sca is NULL");
267                                 return TCORE_RETURN_ENOSYS;
268                         }
269
270                         rtn = po->ops->get_sca(o, ur);
271
272                         break;
273                 case TREQ_SMS_SET_SCA:
274                         if (!po->ops->set_sca)
275                         {
276                                 dbg("[tcore_SMS] ERR: po->ops->set_sca is NULL");
277                                 return TCORE_RETURN_ENOSYS;
278                         }
279
280                         rtn = po->ops->set_sca(o, ur);
281
282                         break;
283                 case TREQ_SMS_GET_CB_CONFIG:
284                         if (!po->ops->get_cb_config)
285                         {
286                                 dbg("[tcore_SMS] ERR: po->ops->get_cb_config is NULL");
287                                 return TCORE_RETURN_ENOSYS;
288                         }
289
290                         rtn = po->ops->get_cb_config(o, ur);
291
292                         break;
293                 case TREQ_SMS_SET_CB_CONFIG:
294                         if (!po->ops->set_cb_config)
295                         {
296                                 dbg("[tcore_SMS] ERR: po->ops->set_cb_config is NULL");
297                                 return TCORE_RETURN_ENOSYS;
298                         }
299
300                         rtn = po->ops->set_cb_config(o, ur);
301
302                         break;
303                 case TREQ_SMS_SET_MEM_STATUS:
304                         if (!po->ops->set_mem_status)
305                         {
306                                 dbg("[tcore_SMS] ERR: po->ops->set_mem_status is NULL");
307                                 return TCORE_RETURN_ENOSYS;
308                         }
309
310                         rtn = po->ops->set_mem_status(o, ur);
311
312                         break;
313                 case TREQ_SMS_GET_PREF_BEARER:
314                         if (!po->ops->get_pref_brearer)
315                         {
316                                 dbg("[tcore_SMS] ERR: po->ops->get_pref_brearer is NULL");
317                                 return TCORE_RETURN_ENOSYS;
318                         }
319
320                         rtn = po->ops->get_pref_brearer(o, ur);
321
322                         break;
323                 case TREQ_SMS_SET_PREF_BEARER:
324                         if (!po->ops->set_pref_brearer)
325                         {
326                                 dbg("[tcore_SMS] ERR: po->ops->get_pref_brearer is NULL");
327                                 return TCORE_RETURN_ENOSYS;
328                         }
329
330                         rtn = po->ops->set_pref_brearer(o, ur);
331
332                         break;
333                 case TREQ_SMS_SET_DELIVERY_REPORT:
334                         if (!po->ops->set_delivery_report)
335                         {
336                                 dbg("[tcore_SMS] ERR: po->ops->set_delivery_report is NULL");
337                                 return TCORE_RETURN_ENOSYS;
338                         }
339
340                         rtn = po->ops->set_delivery_report(o, ur);
341
342                         break;
343                 case TREQ_SMS_SET_MSG_STATUS:
344                         if (!po->ops->set_msg_status)
345                         {
346                                 dbg("[tcore_SMS] ERR: po->ops->set_msg_status is NULL");
347                                 return TCORE_RETURN_ENOSYS;
348                         }
349
350                         rtn = po->ops->set_msg_status(o, ur);
351
352                         break;
353                 case TREQ_SMS_GET_PARAMS:
354                         if (!po->ops->get_sms_params)
355                         {
356                                 dbg("[tcore_SMS] ERR: po->ops->get_sms_params is NULL");
357                                 return TCORE_RETURN_ENOSYS;
358                         }
359
360                         rtn = po->ops->get_sms_params(o, ur);
361
362                         break;
363                 case TREQ_SMS_SET_PARAMS:
364                         if (!po->ops->set_sms_params)
365                         {
366                                 dbg("[tcore_SMS] ERR: po->ops->set_sms_params is NULL");
367                                 return TCORE_RETURN_ENOSYS;
368                         }
369
370                         rtn = po->ops->set_sms_params(o, ur);
371
372                         break;
373                 case TREQ_SMS_GET_PARAMCNT:
374                         if (!po->ops->get_paramcnt)
375                         {
376                                 dbg("[tcore_SMS] ERR: po->ops->get_paramcnt is NULL");
377                                 return TCORE_RETURN_ENOSYS;
378                         }
379
380                         rtn = po->ops->get_paramcnt(o, ur);
381
382                         break;
383                 case TREQ_SMS_SEND_CDMA_MSG:
384                         if (!po->ops->send_cdma_msg)
385                         {
386                                 dbg("[tcore_SMS] ERR: po->ops->send_cdma_msg is NULL");
387                                 return TCORE_RETURN_ENOSYS;
388                         }
389
390                         rtn = po->ops->send_cdma_msg(o, ur);
391
392                         break;
393                 default:
394                         break;
395         }
396
397         dbg("[tcore_SMS] result = [0x%x], command = [0x%x]", rtn, command);
398
399         return rtn;
400 }
401
402 static void _free_hook(CoreObject *o)
403 {
404         struct private_object_data *po = NULL;
405
406         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
407
408         po = tcore_object_ref_object(o);
409         if (po) {
410                 free(po);
411                 tcore_object_link_object(o, NULL);
412         }
413 }
414
415 static void _clone_hook(CoreObject *src, CoreObject *dest)
416 {
417         struct private_object_data *src_po = NULL;
418         struct private_object_data *dest_po = NULL;
419
420         if (!src || !dest)
421                 return;
422
423         dest_po = calloc(sizeof(struct private_object_data), 1);
424         if (!dest_po) {
425                 tcore_object_link_object(dest, NULL);
426                 return;
427         }
428
429         src_po = tcore_object_ref_object(src);
430         dest_po->ops = src_po->ops;
431
432         tcore_object_link_object(dest, dest_po);
433 }
434
435 gboolean tcore_sms_get_ready_status(CoreObject *o)
436 {
437         struct private_object_data *po = NULL;
438         po = tcore_object_ref_object(o);
439         if (!po) {
440                 dbg("po access fail");
441                 return FALSE;
442         }
443
444         return po->b_readyStatus;
445 }
446
447 gboolean tcore_sms_set_ready_status(CoreObject *o, int status)
448 {
449         struct private_object_data *po = NULL;
450         po = tcore_object_ref_object(o);
451         if (!po) {
452                 dbg("po access fail");
453                 return FALSE;
454         }
455
456         po->b_readyStatus = status;
457
458         return TRUE;
459 }
460
461 void tcore_sms_override_ops(CoreObject *o, struct tcore_sms_operations *sms_ops)
462 {
463         struct private_object_data *po = NULL;
464
465         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
466         
467         po = (struct private_object_data *)tcore_object_ref_object(o);
468         if (!po) {
469                 return;
470         }
471
472         if(sms_ops) {
473                 _clone_sms_operations(po, sms_ops);
474         }
475
476         return;
477 }
478
479 CoreObject *tcore_sms_new(TcorePlugin *p, const char *name,
480                 struct tcore_sms_operations *ops, TcoreHal *hal)
481 {
482         CoreObject *o = NULL;
483         struct private_object_data *po = NULL;
484
485         if (!p)
486                 return NULL;
487
488         o = tcore_object_new(p, name, hal);
489         if (!o)
490                 return NULL;
491
492         po = calloc(sizeof(struct private_object_data), 1);
493         if (!po) {
494                 tcore_object_free(o);
495                 return NULL;
496         }
497
498         po->ops = ops;
499
500         tcore_object_set_type(o, CORE_OBJECT_TYPE_SMS);
501         tcore_object_link_object(o, po);
502         tcore_object_set_free_hook(o, _free_hook);
503         tcore_object_set_clone_hook(o, _clone_hook);
504         tcore_object_set_dispatcher(o, _dispatcher);
505
506         return o;
507 }
508
509 CoreObject *tcore_sms_clone(TcorePlugin *p, const char *name, TcoreHal *hal)
510 {
511         CoreObject *o = NULL;
512
513         if (!p)
514                 return NULL;
515
516         o = tcore_object_clone_template_object(p, name, CORE_OBJECT_TYPE_SMS);
517         if (!o)
518                 return NULL;
519
520         tcore_object_set_hal(o, hal);
521         
522         return o;
523 }
524
525 void tcore_sms_free(CoreObject *o)
526 {
527         struct private_object_data *po = NULL;
528
529         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
530
531         po = tcore_object_ref_object(o);
532         if (!po)
533                 return;
534
535         free(po);
536         tcore_object_free(o);
537 }
538
539