co_sms: Fix camel case get_storedMsgCnt and style issue
[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,
133                                         struct tcore_sms_operations *sms_ops)
134 {
135         if (sms_ops->send_umts_msg)
136                 po->ops->send_umts_msg = sms_ops->send_umts_msg;
137
138         if (sms_ops->read_msg)
139                 po->ops->read_msg = sms_ops->read_msg;
140
141         if (sms_ops->save_msg)
142                 po->ops->save_msg = sms_ops->save_msg;
143
144         if (sms_ops->delete_msg)
145                 po->ops->delete_msg = sms_ops->delete_msg;
146
147         if (sms_ops->get_stored_msg_cnt)
148                 po->ops->get_stored_msg_cnt = sms_ops->get_stored_msg_cnt;
149
150         if (sms_ops->get_sca)
151                 po->ops->get_sca = sms_ops->get_sca;
152
153         if (sms_ops->set_sca)
154                 po->ops->set_sca = sms_ops->set_sca;
155
156         if (sms_ops->get_cb_config)
157                 po->ops->get_cb_config = sms_ops->get_cb_config;
158
159         if (sms_ops->set_cb_config)
160                 po->ops->set_cb_config = sms_ops->set_cb_config;
161
162         if (sms_ops->set_mem_status)
163                 po->ops->set_mem_status = sms_ops->set_mem_status;
164
165         if (sms_ops->get_pref_brearer)
166                 po->ops->get_pref_brearer = sms_ops->get_pref_brearer;
167
168         if (sms_ops->set_pref_brearer)
169                 po->ops->set_pref_brearer = sms_ops->set_pref_brearer;
170
171         if (sms_ops->set_delivery_report)
172                 po->ops->set_delivery_report = sms_ops->set_delivery_report;
173
174         if (sms_ops->set_msg_status)
175                 po->ops->set_msg_status = sms_ops->set_msg_status;
176
177         if (sms_ops->get_sms_params)
178                 po->ops->get_sms_params = sms_ops->get_sms_params;
179
180         if (sms_ops->set_sms_params)
181                 po->ops->set_sms_params = sms_ops->set_sms_params;
182
183         if (sms_ops->get_paramcnt)
184                 po->ops->get_paramcnt = sms_ops->get_paramcnt;
185
186         if (sms_ops->send_cdma_msg)
187                 po->ops->send_cdma_msg = sms_ops->send_cdma_msg;
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;
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 (NULL == po || NULL == 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;
209         }
210
211         command = tcore_user_request_get_command(ur);
212         switch (command) {
213         case TREQ_SMS_SEND_UMTS_MSG:
214                 if (NULL == po->ops->send_umts_msg) {
215                         dbg("[tcore_SMS] ERR: po->ops->send_umts_msg is NULL");
216                         return TCORE_RETURN_ENOSYS;
217                 }
218
219                 rtn = po->ops->send_umts_msg(o, ur);
220
221                 break;
222
223         case TREQ_SMS_READ_MSG:
224                 if (NULL == po->ops->read_msg) {
225                         dbg("[tcore_SMS] ERR: po->ops->read_msg is NULL");
226                         return TCORE_RETURN_ENOSYS;
227                 }
228
229                 rtn = po->ops->read_msg(o, ur);
230
231                 break;
232
233         case TREQ_SMS_SAVE_MSG:
234                 if (NULL == po->ops->save_msg) {
235                         dbg("[tcore_SMS] ERR: po->ops->save_msg is NULL");
236                         return TCORE_RETURN_ENOSYS;
237                 }
238
239                 rtn = po->ops->save_msg(o, ur);
240
241                 break;
242
243         case TREQ_SMS_DELETE_MSG:
244                 if (NULL == po->ops->delete_msg) {
245                         dbg("[tcore_SMS] ERR: po->ops->delete_msg is NULL");
246                         return TCORE_RETURN_ENOSYS;
247                 }
248
249                 rtn = po->ops->delete_msg(o, ur);
250
251                 break;
252
253         case TREQ_SMS_GET_COUNT:
254                 if (NULL == po->ops->get_stored_msg_cnt) {
255                         dbg("[tcore_SMS] ERR: po->ops->get_stored_msg_cnt is"
256                                 "NULL");
257                         return TCORE_RETURN_ENOSYS;
258                 }
259
260                 rtn = po->ops->get_stored_msg_cnt(o, ur);
261
262                 break;
263
264         case TREQ_SMS_GET_SCA:
265                 if (NULL == po->ops->get_sca) {
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
274         case TREQ_SMS_SET_SCA:
275                 if (NULL == po->ops->set_sca) {
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
284         case TREQ_SMS_GET_CB_CONFIG:
285                 if (NULL == po->ops->get_cb_config) {
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
294         case TREQ_SMS_SET_CB_CONFIG:
295                 if (NULL == po->ops->set_cb_config) {
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
304         case TREQ_SMS_SET_MEM_STATUS:
305                 if (NULL == po->ops->set_mem_status) {
306                         dbg("[tcore_SMS] ERR: po->ops->set_mem_status is"
307                                 " NULL");
308                         return TCORE_RETURN_ENOSYS;
309                 }
310
311                 rtn = po->ops->set_mem_status(o, ur);
312
313                 break;
314
315         case TREQ_SMS_GET_PREF_BEARER:
316                 if (NULL == po->ops->get_pref_brearer) {
317                         dbg("[tcore_SMS] ERR: po->ops->get_pref_brearer is"
318                                 " NULL");
319                         return TCORE_RETURN_ENOSYS;
320                 }
321
322                 rtn = po->ops->get_pref_brearer(o, ur);
323
324                 break;
325
326         case TREQ_SMS_SET_PREF_BEARER:
327                 if (NULL == po->ops->set_pref_brearer) {
328                         dbg("[tcore_SMS] ERR: po->ops->get_pref_brearer is"
329                                 " NULL");
330                         return TCORE_RETURN_ENOSYS;
331                 }
332
333                 rtn = po->ops->set_pref_brearer(o, ur);
334
335                 break;
336
337         case TREQ_SMS_SET_DELIVERY_REPORT:
338                 if (!po->ops->set_delivery_report) {
339                         dbg("[tcore_SMS] ERR: po->ops->set_delivery_report is"
340                                 " NULL");
341                         return TCORE_RETURN_ENOSYS;
342                 }
343
344                 rtn = po->ops->set_delivery_report(o, ur);
345
346                 break;
347
348         case TREQ_SMS_SET_MSG_STATUS:
349                 if (NULL == po->ops->set_msg_status) {
350                         dbg("[tcore_SMS] ERR: po->ops->set_msg_status is"
351                                 " NULL");
352                         return TCORE_RETURN_ENOSYS;
353                 }
354
355                 rtn = po->ops->set_msg_status(o, ur);
356
357                 break;
358
359         case TREQ_SMS_GET_PARAMS:
360                 if (NULL == po->ops->get_sms_params) {
361                         dbg("[tcore_SMS] ERR: po->ops->get_sms_params is"
362                                 " NULL");
363                         return TCORE_RETURN_ENOSYS;
364                 }
365
366                 rtn = po->ops->get_sms_params(o, ur);
367
368                 break;
369
370         case TREQ_SMS_SET_PARAMS:
371                 if (NULL == po->ops->set_sms_params) {
372                         dbg("[tcore_SMS] ERR: po->ops->set_sms_params is"
373                                 " NULL");
374                         return TCORE_RETURN_ENOSYS;
375                 }
376
377                 rtn = po->ops->set_sms_params(o, ur);
378
379                 break;
380
381         case TREQ_SMS_GET_PARAMCNT:
382                 if (NULL == po->ops->get_paramcnt) {
383                         dbg("[tcore_SMS] ERR: po->ops->get_paramcnt is NULL");
384                         return TCORE_RETURN_ENOSYS;
385                 }
386
387                 rtn = po->ops->get_paramcnt(o, ur);
388
389                 break;
390
391         case TREQ_SMS_SEND_CDMA_MSG:
392                 if (NULL == po->ops->send_cdma_msg) {
393                         dbg("[tcore_SMS] ERR: po->ops->send_cdma_msg is NULL");
394                         return TCORE_RETURN_ENOSYS;
395                 }
396
397                 rtn = po->ops->send_cdma_msg(o, ur);
398
399                 break;
400
401         default:
402                 break;
403         }
404
405         dbg("[tcore_SMS] result = [0x%x], command = [0x%x]", rtn, command);
406
407         return rtn;
408 }
409
410 static void _free_hook(CoreObject *o)
411 {
412         struct private_object_data *po = NULL;
413
414         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
415
416         po = tcore_object_ref_object(o);
417         if (po) {
418                 free(po);
419                 tcore_object_link_object(o, NULL);
420         }
421 }
422
423 static void _clone_hook(CoreObject *src, CoreObject *dest)
424 {
425         struct private_object_data *src_po = NULL;
426         struct private_object_data *dest_po = NULL;
427
428         if (!src || !dest)
429                 return;
430
431         dest_po = calloc(sizeof(struct private_object_data), 1);
432         if (!dest_po) {
433                 tcore_object_link_object(dest, NULL);
434                 return;
435         }
436
437         src_po = tcore_object_ref_object(src);
438         dest_po->ops = src_po->ops;
439
440         tcore_object_link_object(dest, dest_po);
441 }
442
443 gboolean tcore_sms_get_ready_status(CoreObject *o)
444 {
445         struct private_object_data *po = NULL;
446         po = tcore_object_ref_object(o);
447         if (!po) {
448                 dbg("po access fail");
449                 return FALSE;
450         }
451
452         return po->b_readyStatus;
453 }
454
455 gboolean tcore_sms_set_ready_status(CoreObject *o, int status)
456 {
457         struct private_object_data *po = NULL;
458         po = tcore_object_ref_object(o);
459         if (!po) {
460                 dbg("po access fail");
461                 return FALSE;
462         }
463
464         po->b_readyStatus = status;
465
466         return TRUE;
467 }
468
469 void tcore_sms_override_ops(CoreObject *o, struct tcore_sms_operations *sms_ops)
470 {
471         struct private_object_data *po = NULL;
472
473         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
474         
475         po = (struct private_object_data *)tcore_object_ref_object(o);
476         if (!po) {
477                 return;
478         }
479
480         if(sms_ops) {
481                 _clone_sms_operations(po, sms_ops);
482         }
483
484         return;
485 }
486
487 CoreObject *tcore_sms_new(TcorePlugin *p, const char *name,
488                 struct tcore_sms_operations *ops, TcoreHal *hal)
489 {
490         CoreObject *o = NULL;
491         struct private_object_data *po = NULL;
492
493         if (!p)
494                 return NULL;
495
496         o = tcore_object_new(p, name, hal);
497         if (!o)
498                 return NULL;
499
500         po = calloc(sizeof(struct private_object_data), 1);
501         if (!po) {
502                 tcore_object_free(o);
503                 return NULL;
504         }
505
506         po->ops = ops;
507
508         tcore_object_set_type(o, CORE_OBJECT_TYPE_SMS);
509         tcore_object_link_object(o, po);
510         tcore_object_set_free_hook(o, _free_hook);
511         tcore_object_set_clone_hook(o, _clone_hook);
512         tcore_object_set_dispatcher(o, _dispatcher);
513
514         return o;
515 }
516
517 CoreObject *tcore_sms_clone(TcorePlugin *p, const char *name, TcoreHal *hal)
518 {
519         CoreObject *o = NULL;
520
521         if (!p)
522                 return NULL;
523
524         o = tcore_object_clone_template_object(p, name, CORE_OBJECT_TYPE_SMS);
525         if (!o)
526                 return NULL;
527
528         tcore_object_set_hal(o, hal);
529         
530         return o;
531 }
532
533 void tcore_sms_free(CoreObject *o)
534 {
535         struct private_object_data *po = NULL;
536
537         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
538
539         po = tcore_object_ref_object(o);
540         if (!po)
541                 return;
542
543         free(po);
544         tcore_object_free(o);
545 }
546
547