2fd96dcab2e43bf984bd61203d05472ae10c173d
[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 TReturn _dispatcher(CoreObject *o, UserRequest *ur)
133 {
134         enum tcore_request_command command;
135         struct private_object_data *po = NULL;
136         TReturn rtn = TCORE_RETURN_SUCCESS;
137
138         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_SMS, TCORE_RETURN_EINVAL);
139
140         po = tcore_object_ref_object(o);
141         if (!po || !po->ops) {
142                 dbg("[tcore_SMS] ERR: private_object is NULL or ops is NULL");
143                 return TCORE_RETURN_ENOSYS;
144         }
145
146         if(po->b_readyStatus == FALSE) {
147                 dbg("[tcore_SMS] DEVICE_NOT_READY");
148                 return TCORE_RETURN_ENOSYS; /* TAPI_API_NETTEXT_DEVICE_NOT_READY */
149         }
150
151         command = tcore_user_request_get_command(ur);
152         switch (command) {
153                 case TREQ_SMS_SEND_UMTS_MSG:
154                         if (!po->ops->send_umts_msg)
155                         {
156                                 dbg("[tcore_SMS] ERR: po->ops->send_umts_msg is NULL");
157                                 return TCORE_RETURN_ENOSYS;
158                         }
159
160                         rtn = po->ops->send_umts_msg(o, ur);
161
162                         break;
163                 case TREQ_SMS_READ_MSG:
164                         if (!po->ops->read_msg)
165                         {
166                                 dbg("[tcore_SMS] ERR: po->ops->read_msg is NULL");
167                                 return TCORE_RETURN_ENOSYS;
168                         }
169
170                         rtn = po->ops->read_msg(o, ur);
171
172                         break;
173                 case TREQ_SMS_SAVE_MSG:
174                         if (!po->ops->save_msg)
175                         {
176                                 dbg("[tcore_SMS] ERR: po->ops->save_msg is NULL");
177                                 return TCORE_RETURN_ENOSYS;
178                         }
179
180                         rtn = po->ops->save_msg(o, ur);
181
182                         break;
183                 case TREQ_SMS_DELETE_MSG:
184                         if (!po->ops->delete_msg)
185                         {
186                                 dbg("[tcore_SMS] ERR: po->ops->delete_msg is NULL");
187                                 return TCORE_RETURN_ENOSYS;
188                         }
189
190                         rtn = po->ops->delete_msg(o, ur);
191
192                         break;
193                 case TREQ_SMS_GET_COUNT:
194                         if (!po->ops->get_storedMsgCnt)
195                         {
196                                 dbg("[tcore_SMS] ERR: po->ops->get_storedMsgCnt is NULL");
197                                 return TCORE_RETURN_ENOSYS;
198                         }
199
200                         rtn = po->ops->get_storedMsgCnt(o, ur);
201
202                         break;
203                 case TREQ_SMS_GET_SCA:
204                         if (!po->ops->get_sca)
205                         {
206                                 dbg("[tcore_SMS] ERR: po->ops->get_sca is NULL");
207                                 return TCORE_RETURN_ENOSYS;
208                         }
209
210                         rtn = po->ops->get_sca(o, ur);
211
212                         break;
213                 case TREQ_SMS_SET_SCA:
214                         if (!po->ops->set_sca)
215                         {
216                                 dbg("[tcore_SMS] ERR: po->ops->set_sca is NULL");
217                                 return TCORE_RETURN_ENOSYS;
218                         }
219
220                         rtn = po->ops->set_sca(o, ur);
221
222                         break;
223                 case TREQ_SMS_GET_CB_CONFIG:
224                         if (!po->ops->get_cb_config)
225                         {
226                                 dbg("[tcore_SMS] ERR: po->ops->get_cb_config is NULL");
227                                 return TCORE_RETURN_ENOSYS;
228                         }
229
230                         rtn = po->ops->get_cb_config(o, ur);
231
232                         break;
233                 case TREQ_SMS_SET_CB_CONFIG:
234                         if (!po->ops->set_cb_config)
235                         {
236                                 dbg("[tcore_SMS] ERR: po->ops->set_cb_config is NULL");
237                                 return TCORE_RETURN_ENOSYS;
238                         }
239
240                         rtn = po->ops->set_cb_config(o, ur);
241
242                         break;
243                 case TREQ_SMS_SET_MEM_STATUS:
244                         if (!po->ops->set_mem_status)
245                         {
246                                 dbg("[tcore_SMS] ERR: po->ops->set_mem_status is NULL");
247                                 return TCORE_RETURN_ENOSYS;
248                         }
249
250                         rtn = po->ops->set_mem_status(o, ur);
251
252                         break;
253                 case TREQ_SMS_GET_PREF_BEARER:
254                         if (!po->ops->get_pref_brearer)
255                         {
256                                 dbg("[tcore_SMS] ERR: po->ops->get_pref_brearer is NULL");
257                                 return TCORE_RETURN_ENOSYS;
258                         }
259
260                         rtn = po->ops->get_pref_brearer(o, ur);
261
262                         break;
263                 case TREQ_SMS_SET_PREF_BEARER:
264                         if (!po->ops->set_pref_brearer)
265                         {
266                                 dbg("[tcore_SMS] ERR: po->ops->get_pref_brearer is NULL");
267                                 return TCORE_RETURN_ENOSYS;
268                         }
269
270                         rtn = po->ops->set_pref_brearer(o, ur);
271
272                         break;
273                 case TREQ_SMS_SET_DELIVERY_REPORT:
274                         if (!po->ops->set_delivery_report)
275                         {
276                                 dbg("[tcore_SMS] ERR: po->ops->set_delivery_report is NULL");
277                                 return TCORE_RETURN_ENOSYS;
278                         }
279
280                         rtn = po->ops->set_delivery_report(o, ur);
281
282                         break;
283                 case TREQ_SMS_SET_MSG_STATUS:
284                         if (!po->ops->set_msg_status)
285                         {
286                                 dbg("[tcore_SMS] ERR: po->ops->set_msg_status is NULL");
287                                 return TCORE_RETURN_ENOSYS;
288                         }
289
290                         rtn = po->ops->set_msg_status(o, ur);
291
292                         break;
293                 case TREQ_SMS_GET_PARAMS:
294                         if (!po->ops->get_sms_params)
295                         {
296                                 dbg("[tcore_SMS] ERR: po->ops->get_sms_params is NULL");
297                                 return TCORE_RETURN_ENOSYS;
298                         }
299
300                         rtn = po->ops->get_sms_params(o, ur);
301
302                         break;
303                 case TREQ_SMS_SET_PARAMS:
304                         if (!po->ops->set_sms_params)
305                         {
306                                 dbg("[tcore_SMS] ERR: po->ops->set_sms_params is NULL");
307                                 return TCORE_RETURN_ENOSYS;
308                         }
309
310                         rtn = po->ops->set_sms_params(o, ur);
311
312                         break;
313                 case TREQ_SMS_GET_PARAMCNT:
314                         if (!po->ops->get_paramcnt)
315                         {
316                                 dbg("[tcore_SMS] ERR: po->ops->get_paramcnt is NULL");
317                                 return TCORE_RETURN_ENOSYS;
318                         }
319
320                         rtn = po->ops->get_paramcnt(o, ur);
321
322                         break;
323                 case TREQ_SMS_SEND_CDMA_MSG:
324                         if (!po->ops->send_cdma_msg)
325                         {
326                                 dbg("[tcore_SMS] ERR: po->ops->send_cdma_msg is NULL");
327                                 return TCORE_RETURN_ENOSYS;
328                         }
329
330                         rtn = po->ops->send_cdma_msg(o, ur);
331
332                         break;
333                 default:
334                         break;
335         }
336
337         dbg("[tcore_SMS] result = [0x%x], command = [0x%x]", rtn, command);
338
339         return rtn;
340 }
341
342 static void _free_hook(CoreObject *o)
343 {
344         struct private_object_data *po = NULL;
345
346         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
347
348         po = tcore_object_ref_object(o);
349         if (po) {
350                 free(po);
351                 tcore_object_link_object(o, NULL);
352         }
353 }
354
355 static void _clone_hook(CoreObject *src, CoreObject *dest)
356 {
357         struct private_object_data *src_po = NULL;
358         struct private_object_data *dest_po = NULL;
359
360         if (!src || !dest)
361                 return;
362
363         dest_po = calloc(sizeof(struct private_object_data), 1);
364         if (!dest_po) {
365                 tcore_object_link_object(dest, NULL);
366                 return;
367         }
368
369         src_po = tcore_object_ref_object(src);
370         dest_po->ops = src_po->ops;
371
372         tcore_object_link_object(dest, dest_po);
373 }
374
375 gboolean tcore_sms_get_ready_status(CoreObject *o)
376 {
377         struct private_object_data *po = NULL;
378         po = tcore_object_ref_object(o);
379         if (!po) {
380                 dbg("po access fail");
381                 return FALSE;
382         }
383
384         return po->b_readyStatus;
385 }
386
387 gboolean tcore_sms_set_ready_status(CoreObject *o, int status)
388 {
389         struct private_object_data *po = NULL;
390         po = tcore_object_ref_object(o);
391         if (!po) {
392                 dbg("po access fail");
393                 return FALSE;
394         }
395
396         po->b_readyStatus = status;
397
398         return TRUE;
399 }
400
401 CoreObject *tcore_sms_new(TcorePlugin *p, const char *name,
402                 struct tcore_sms_operations *ops, TcoreHal *hal)
403 {
404         CoreObject *o = NULL;
405         struct private_object_data *po = NULL;
406
407         if (!p)
408                 return NULL;
409
410         o = tcore_object_new(p, name, hal);
411         if (!o)
412                 return NULL;
413
414         po = calloc(sizeof(struct private_object_data), 1);
415         if (!po) {
416                 tcore_object_free(o);
417                 return NULL;
418         }
419
420         po->ops = ops;
421
422         tcore_object_set_type(o, CORE_OBJECT_TYPE_SMS);
423         tcore_object_link_object(o, po);
424         tcore_object_set_free_hook(o, _free_hook);
425         tcore_object_set_clone_hook(o, _clone_hook);
426         tcore_object_set_dispatcher(o, _dispatcher);
427
428         return o;
429 }
430
431 void tcore_sms_free(CoreObject *o)
432 {
433         struct private_object_data *po = NULL;
434
435         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SMS);
436
437         po = tcore_object_ref_object(o);
438         if (!po)
439                 return;
440
441         free(po);
442         tcore_object_free(o);
443 }
444
445