wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Systeminfo / JSSIMInfo.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <memory>
19 #include <JSTizenExceptionFactory.h>
20 #include <JSTizenException.h>
21 #include <SecurityExceptions.h>
22 #include <sim.h>
23 #include "JSSIMInfo.h"
24 #include "plugin_config.h"
25 #include <Logger.h>
26
27 namespace DeviceAPI {
28 namespace Systeminfo {
29 using namespace WrtDeviceApis::CommonsJavaScript;
30 using namespace WrtDeviceApis::Commons;
31 using namespace DeviceAPI::Common;
32
33 namespace {
34 const char* SIM_STATE_PROPERTY = "state";
35 const char* SIM_OPERATORNAME_PROPERTY = "operatorName";
36 const char* SIM_MSISDN_PROPERTY = "msisdn";
37 const char* SIM_ICCID_PROPERTY = "iccid";
38 const char* SIM_MCC_PROPERTY = "mcc";
39 const char* SIM_MNC_PROPERTY = "mnc";
40 const char* SIM_MSIN_PROPERTY = "msin";
41 const char* SIM_SPN_PROPERTY = "spn";
42
43
44 JSClassRef JSSIMInfo::m_classRef = NULL;
45
46 JSClassDefinition JSSIMInfo::m_classInfo = {
47     0,
48     kJSClassAttributeNone,
49     "siminfo",
50     0,
51     m_properties,
52     NULL,
53     Initialize,
54     Finalize,
55     hasProperty,
56     getProperty,
57     NULL,
58     NULL,
59     NULL,
60     NULL,
61     NULL,
62     NULL,
63     NULL
64 };
65
66 JSStaticValue JSSIMInfo::m_properties[] = {
67     { SIM_STATE_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
68     { SIM_OPERATORNAME_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
69     { SIM_MSISDN_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
70     { SIM_ICCID_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },        
71     { SIM_MCC_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },        
72     { SIM_MNC_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },                
73     { SIM_MSIN_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
74     { SIM_SPN_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },        
75     { 0, 0, 0, 0 }
76 };
77
78 const JSClassRef JSSIMInfo::getClassRef()
79 {
80     if (!m_classRef) {
81         m_classRef = JSClassCreate(&m_classInfo);
82     }
83     return m_classRef;
84 }
85
86 const JSClassDefinition* JSSIMInfo::getClassInfo()
87 {
88     return &m_classInfo;
89 }
90
91 void JSSIMInfo::Initialize(JSContextRef context, JSObjectRef object)
92 {
93 }
94
95 void JSSIMInfo::Finalize(JSObjectRef object)
96 {
97     LoggerD("Entered");
98     JSSIMPriv* priv = static_cast<JSSIMPriv*>(JSObjectGetPrivate(object));
99     JSObjectSetPrivate(object, NULL);
100     LoggerD("Deleting SIM Info object");
101     delete priv;
102 }
103
104 bool JSSIMInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
105 {
106     return JSUtils::hasProperty(m_properties, propertyName);
107 }
108
109 JSObjectRef JSSIMInfo::createJSObject(JSContextRef context, const SIMPropertiesPtr SIMInfo)
110 {
111     LoggerD("Enter");
112     JSSIMPriv *priv = new JSSIMPriv(context, SIMInfo);
113     return JSObjectMake(context, getClassRef(), priv);
114 }
115
116 JSValueRef JSSIMInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
117 {
118     LoggerD("Enter");
119     JSSIMPriv *priv = static_cast<JSSIMPriv*>(JSObjectGetPrivate(object));
120     int ret = SIM_ERROR_NONE;
121     if (NULL == priv) {
122         LoggerE("Private object not set.");
123         return JSValueMakeUndefined(context);
124     }
125
126     Try
127     {
128         SIMPropertiesPtr SIMInfo = priv->getObject();
129         Converter convert(context);
130         if (JSStringIsEqualToUTF8CString(propertyName, SIM_STATE_PROPERTY)) {
131             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_SIM_VALUE);
132             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
133             sim_state_e state;
134             ret = sim_get_state(&state);
135             if (ret == SIM_ERROR_NONE) {
136                 if (state == SIM_STATE_UNAVAILABLE) {
137                     SIMInfo->state = "ABSENT";
138                 } else if (state == SIM_STATE_LOCKED) {
139                    SIMInfo->state = "PIN_REQUIRED";
140                 } else if (state == SIM_STATE_AVAILABLE) {
141                    SIMInfo->state = "READY";
142                 } else if (state == SIM_STATE_UNKNOWN) {
143                    SIMInfo->state = "INITIALIZING";
144                 }
145             }
146             return convert.toJSValueRef(SIMInfo->state);
147         } else if (JSStringIsEqualToUTF8CString(propertyName, SIM_OPERATORNAME_PROPERTY)) {
148             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_SIM_VALUE);
149             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
150             char* fullName = NULL;
151             char* shortName = NULL;
152             ret = sim_get_cphs_operator_name(&fullName, &shortName);
153             if (ret == SIM_ERROR_NONE) {
154                 if (fullName) {
155                     SIMInfo->operatorName = fullName;
156                     LoggerD("operatorName : " << SIMInfo->operatorName);
157                     free(fullName);
158                 } else if (shortName) {
159                     SIMInfo->operatorName = shortName;
160                     LoggerD("operatorName : " << SIMInfo->operatorName);
161                     free(shortName);
162                 }
163             }
164             return convert.toJSValueRef(SIMInfo->operatorName);
165         } else if (JSStringIsEqualToUTF8CString(propertyName, SIM_MSISDN_PROPERTY)) {
166             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_PARTNER_VALUE);
167             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
168             char* msisdn = NULL;
169             ret = sim_get_subscriber_number(&msisdn);
170             if (ret == SIM_ERROR_NONE) {
171                 if (msisdn) {
172                     SIMInfo->msisdn = msisdn;
173                     LoggerD("msisdn : " << SIMInfo->msisdn);
174                     free(msisdn);
175                 }
176             }
177             return convert.toJSValueRef(SIMInfo->msisdn);
178         } else if (JSStringIsEqualToUTF8CString(propertyName, SIM_ICCID_PROPERTY)) {
179             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_SIM_VALUE);
180             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
181             char* iccId = NULL;
182             ret = sim_get_icc_id(&iccId);
183             if (ret == SIM_ERROR_NONE) {
184                 if (iccId) {
185                     SIMInfo->iccid = iccId;
186                     LoggerD("iccid : " << SIMInfo->iccid);
187                     free(iccId);
188                 }
189             }
190             return convert.toJSValueRef(SIMInfo->iccid);
191         } else if (JSStringIsEqualToUTF8CString(propertyName, SIM_MCC_PROPERTY)) {
192             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_SIM_VALUE);
193             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
194             char* mcc = NULL;
195             ret = sim_get_mcc(&mcc);
196             if (ret == SIM_ERROR_NONE) {
197                 if (mcc) {
198                     SIMInfo->mcc = atoi(mcc);
199                     LoggerD("mcc : " << SIMInfo->mcc);
200                     free(mcc);
201                 }
202             }
203             return convert.toJSValueRef(SIMInfo->mcc);
204         } else if (JSStringIsEqualToUTF8CString(propertyName, SIM_MNC_PROPERTY)) {
205             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_SIM_VALUE);
206             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
207             char* mnc = NULL;
208             ret = sim_get_mnc(&mnc);
209             if (ret == SIM_ERROR_NONE) {
210                 if (mnc) {
211                     SIMInfo->mnc = atoi(mnc);
212                     LoggerD("mnc : " << SIMInfo->mnc);
213                     free(mnc);
214                 }
215             }
216             return convert.toJSValueRef(SIMInfo->mnc);
217         } else if (JSStringIsEqualToUTF8CString(propertyName, SIM_MSIN_PROPERTY)) {
218             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_PARTNER_VALUE);
219             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
220             char* msin = NULL;
221             ret = sim_get_msin(&msin);
222             if (ret == SIM_ERROR_NONE) {
223                 if (msin) {
224                     SIMInfo->msin = msin;
225                     LoggerD("msin : " << SIMInfo->msin);
226                     free(msin);
227                 }
228             }
229             return convert.toJSValueRef(SIMInfo->msin);
230         } else if (JSStringIsEqualToUTF8CString(propertyName, SIM_SPN_PROPERTY)) {
231             AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_GET_PROPERTY_SIM_VALUE);
232             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
233             char* spn = NULL;
234             ret = sim_get_spn(&spn);
235             if (ret == SIM_ERROR_NONE) {
236                 if (spn) {
237                     SIMInfo->spn = spn;
238                     LoggerD("spn : " << SIMInfo->spn);
239                     free(spn);
240                 }
241             }
242             return convert.toJSValueRef(SIMInfo->spn);
243         }            
244     }
245     Catch(Exception)
246     {
247         LoggerE("Exception: " << _rethrown_exception.GetMessage());
248     }
249     return JSValueMakeUndefined(context);
250 }
251 }
252 }