fixed build error; fixed compilation warnings
[public/netxms.git] / src / server / core / mobile.cpp
CommitLineData
4899db4d 1/*
f22c4f7c 2** NetXMS - Network Management System
eb0c0312 3** Copyright (C) 2003-2013 Victor Kirhenshtein
f22c4f7c
VK
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: mobile.cpp
f22c4f7c
VK
20**/
21
22#include "nxcore.h"
23
24/**
25 * Default constructor
26 */
6fd6de0a 27MobileDevice::MobileDevice() : DataCollectionTarget()
f22c4f7c
VK
28{
29 m_lastReportTime = 0;
30 m_deviceId = NULL;
31 m_vendor = NULL;
32 m_model = NULL;
33 m_serialNumber = NULL;
34 m_osName = NULL;
35 m_osVersion = NULL;
36 m_userId = NULL;
171c2fd6 37 m_batteryLevel = -1;
f22c4f7c
VK
38}
39
40/**
41 * Constructor for creating new mobile device object
42 */
6fd6de0a 43MobileDevice::MobileDevice(const TCHAR *name, const TCHAR *deviceId) : DataCollectionTarget(name)
f22c4f7c
VK
44{
45 m_lastReportTime = 0;
46 m_deviceId = _tcsdup(deviceId);
47 m_vendor = NULL;
48 m_model = NULL;
49 m_serialNumber = NULL;
50 m_osName = NULL;
51 m_osVersion = NULL;
52 m_userId = NULL;
171c2fd6 53 m_batteryLevel = -1;
f22c4f7c
VK
54}
55
56/**
57 * Destructor
58 */
59MobileDevice::~MobileDevice()
60{
61 safe_free(m_deviceId);
62 safe_free(m_vendor);
63 safe_free(m_model);
64 safe_free(m_serialNumber);
65 safe_free(m_osName);
66 safe_free(m_osVersion);
67 safe_free(m_userId);
68}
9aa67910
VK
69
70/**
71 * Create object from database data
72 */
c42b4551 73BOOL MobileDevice::loadFromDatabase(UINT32 dwId)
9aa67910 74{
c42b4551 75 m_id = dwId;
9aa67910
VK
76
77 if (!loadCommonProperties())
78 {
4dd963e9 79 DbgPrintf(2, _T("Cannot load common properties for mobile device object %d"), dwId);
9aa67910
VK
80 return FALSE;
81 }
82
83 TCHAR query[256];
c42b4551 84 _sntprintf(query, 256, _T("SELECT device_id,vendor,model,serial_number,os_name,os_version,user_id,battery_level FROM mobile_devices WHERE id=%d"), (int)m_id);
9aa67910
VK
85 DB_RESULT hResult = DBSelect(g_hCoreDB, query);
86 if (hResult == NULL)
87 return FALSE;
88
89 m_deviceId = DBGetField(hResult, 0, 0, NULL, 0);
90 m_vendor = DBGetField(hResult, 0, 1, NULL, 0);
91 m_model = DBGetField(hResult, 0, 2, NULL, 0);
92 m_serialNumber = DBGetField(hResult, 0, 3, NULL, 0);
93 m_osName = DBGetField(hResult, 0, 4, NULL, 0);
94 m_osVersion = DBGetField(hResult, 0, 5, NULL, 0);
95 m_userId = DBGetField(hResult, 0, 6, NULL, 0);
171c2fd6 96 m_batteryLevel = DBGetFieldLong(hResult, 0, 7);
9aa67910
VK
97 DBFreeResult(hResult);
98
99 // Load DCI and access list
100 loadACLFromDB();
101 loadItemsFromDB();
102 for(int i = 0; i < m_dcObjects->size(); i++)
103 if (!m_dcObjects->get(i)->loadThresholdsFromDB())
104 return FALSE;
105
106 return TRUE;
107}
108
109/**
110 * Save object to database
111 */
c42b4551 112BOOL MobileDevice::saveToDatabase(DB_HANDLE hdb)
9aa67910
VK
113{
114 // Lock object's access
c42b4551 115 lockProperties();
9aa67910
VK
116
117 saveCommonProperties(hdb);
118
119 BOOL bResult;
120 DB_STATEMENT hStmt;
c42b4551 121 if (IsDatabaseRecordExist(hdb, _T("mobile_devices"), _T("id"), m_id))
171c2fd6 122 hStmt = DBPrepare(hdb, _T("UPDATE mobile_devices SET device_id=?,vendor=?,model=?,serial_number=?,os_name=?,os_version=?,user_id=?,battery_level=? WHERE id=?"));
9aa67910 123 else
171c2fd6 124 hStmt = DBPrepare(hdb, _T("INSERT INTO mobile_devices (device_id,vendor,model,serial_number,os_name,os_version,user_id,battery_level,id) VALUES (?,?,?,?,?,?,?,?,?)"));
9aa67910
VK
125 if (hStmt != NULL)
126 {
127 DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_deviceId), DB_BIND_STATIC);
128 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_vendor), DB_BIND_STATIC);
129 DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_model), DB_BIND_STATIC);
130 DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_serialNumber), DB_BIND_STATIC);
131 DBBind(hStmt, 5, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_osName), DB_BIND_STATIC);
132 DBBind(hStmt, 6, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_osVersion), DB_BIND_STATIC);
133 DBBind(hStmt, 7, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_userId), DB_BIND_STATIC);
171c2fd6 134 DBBind(hStmt, 8, DB_SQLTYPE_INTEGER, m_batteryLevel);
c42b4551 135 DBBind(hStmt, 9, DB_SQLTYPE_INTEGER, m_id);
9aa67910
VK
136
137 bResult = DBExecute(hStmt);
138
139 DBFreeStatement(hStmt);
140 }
141 else
142 {
143 bResult = FALSE;
144 }
145
146 // Save data collection items
147 if (bResult)
148 {
b06436f4 149 lockDciAccess(false);
9aa67910
VK
150 for(int i = 0; i < m_dcObjects->size(); i++)
151 m_dcObjects->get(i)->saveToDB(hdb);
152 unlockDciAccess();
153 }
154
155 // Save access list
156 saveACLToDB(hdb);
157
158 // Clear modifications flag and unlock object
159 if (bResult)
01152a54 160 m_isModified = false;
c42b4551 161 unlockProperties();
9aa67910
VK
162
163 return bResult;
164}
165
166/**
167 * Delete object from database
168 */
c42b4551 169bool MobileDevice::deleteFromDatabase(DB_HANDLE hdb)
9aa67910 170{
c42b4551 171 bool success = DataCollectionTarget::deleteFromDatabase(hdb);
22ee6d97
VK
172 if (success)
173 success = executeQueryOnObject(hdb, _T("DELETE FROM mobile_devices WHERE id=?"));
174 return success;
9aa67910
VK
175}
176
177/**
178 * Create CSCP message with object's data
179 */
c42b4551 180void MobileDevice::fillMessage(CSCPMessage *msg)
9aa67910 181{
c42b4551 182 DataCollectionTarget::fillMessage(msg);
171c2fd6
VK
183 msg->SetVariable(VID_DEVICE_ID, CHECK_NULL_EX(m_deviceId));
184 msg->SetVariable(VID_VENDOR, CHECK_NULL_EX(m_vendor));
185 msg->SetVariable(VID_MODEL, CHECK_NULL_EX(m_model));
6583a0f8
VK
186 msg->SetVariable(VID_SERIAL_NUMBER, CHECK_NULL_EX(m_serialNumber));
187 msg->SetVariable(VID_OS_NAME, CHECK_NULL_EX(m_osName));
188 msg->SetVariable(VID_OS_VERSION, CHECK_NULL_EX(m_osVersion));
171c2fd6 189 msg->SetVariable(VID_USER_ID, CHECK_NULL_EX(m_userId));
967893bb 190 msg->SetVariable(VID_BATTERY_LEVEL, (UINT32)m_batteryLevel);
eb0c0312 191 msg->SetVariable(VID_LAST_CHANGE_TIME, (QWORD)m_lastReportTime);
9aa67910
VK
192}
193
194/**
195 * Modify object from message
196 */
c42b4551 197UINT32 MobileDevice::modifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
9aa67910
VK
198{
199 if (!bAlreadyLocked)
c42b4551 200 lockProperties();
9aa67910 201
c42b4551 202 return DataCollectionTarget::modifyFromMessage(pRequest, TRUE);
9aa67910 203}
6583a0f8
VK
204
205/**
206 * Update system information from NXCP message
207 */
208void MobileDevice::updateSystemInfo(CSCPMessage *msg)
209{
c42b4551 210 lockProperties();
6583a0f8 211
63862632
VK
212 m_lastReportTime = time(NULL);
213
6583a0f8
VK
214 safe_free(m_vendor);
215 m_vendor = msg->GetVariableStr(VID_VENDOR);
216
217 safe_free(m_model);
218 m_model = msg->GetVariableStr(VID_MODEL);
219
220 safe_free(m_serialNumber);
221 m_serialNumber = msg->GetVariableStr(VID_SERIAL_NUMBER);
222
223 safe_free(m_osName);
224 m_osName = msg->GetVariableStr(VID_OS_NAME);
225
226 safe_free(m_osVersion);
227 m_osVersion = msg->GetVariableStr(VID_OS_VERSION);
228
229 safe_free(m_userId);
230 m_userId = msg->GetVariableStr(VID_USER_NAME);
231
c42b4551
VK
232 setModified();
233 unlockProperties();
6583a0f8
VK
234}
235
236/**
237 * Update status from NXCP message
238 */
239void MobileDevice::updateStatus(CSCPMessage *msg)
240{
c42b4551 241 lockProperties();
6583a0f8 242
63862632
VK
243 m_lastReportTime = time(NULL);
244
4101571e 245 int type = msg->getFieldType(VID_BATTERY_LEVEL);
30b86580 246 if (type == CSCP_DT_INT32)
6583a0f8 247 m_batteryLevel = (int)msg->GetVariableLong(VID_BATTERY_LEVEL);
4101571e
VK
248 else if (type == CSCP_DT_INT16)
249 m_batteryLevel = (int)msg->GetVariableShort(VID_BATTERY_LEVEL);
6583a0f8
VK
250 else
251 m_batteryLevel = -1;
252
5c44534b 253 if (msg->isFieldExist(VID_GEOLOCATION_TYPE))
4899db4d 254 {
6583a0f8 255 m_geoLocation = GeoLocation(*msg);
4899db4d 256 addLocationToHistory();
257 }
6583a0f8 258
5c44534b 259 if (msg->isFieldExist(VID_IP_ADDRESS))
6583a0f8
VK
260 m_dwIpAddr = msg->GetVariableLong(VID_IP_ADDRESS);
261
262 TCHAR temp[32];
263 DbgPrintf(5, _T("Mobile device %s [%d] updated from agent (battery=%d addr=%s loc=[%s %s])"),
c42b4551 264 m_name, (int)m_id, m_batteryLevel, IpToStr(m_dwIpAddr, temp),
6583a0f8
VK
265 m_geoLocation.getLatitudeAsString(), m_geoLocation.getLongitudeAsString());
266
c42b4551
VK
267 setModified();
268 unlockProperties();
6583a0f8 269}
eb0c0312
VK
270
271/**
272 * Get value for server's internal parameter
273 */
17b1ab4a 274UINT32 MobileDevice::getInternalItem(const TCHAR *param, size_t bufSize, TCHAR *buffer)
eb0c0312 275{
967893bb 276 UINT32 rc = DataCollectionTarget::getInternalItem(param, bufSize, buffer);
eb0c0312
VK
277 if (rc == DCE_SUCCESS)
278 return DCE_SUCCESS;
279 rc = DCE_SUCCESS;
280
281 if (!_tcsicmp(param, _T("MobileDevice.BatteryLevel")))
282 {
283 _sntprintf(buffer, bufSize, _T("%d"), m_batteryLevel);
284 }
285 else if (!_tcsicmp(param, _T("MobileDevice.DeviceId")))
286 {
287 nx_strncpy(buffer, CHECK_NULL_EX(m_deviceId), bufSize);
288 }
289 else if (!_tcsicmp(param, _T("MobileDevice.LastReportTime")))
290 {
291 _sntprintf(buffer, bufSize, INT64_FMT, (INT64)m_lastReportTime);
292 }
293 else if (!_tcsicmp(param, _T("MobileDevice.Model")))
294 {
295 nx_strncpy(buffer, CHECK_NULL_EX(m_model), bufSize);
296 }
297 else if (!_tcsicmp(param, _T("MobileDevice.OS.Name")))
298 {
299 nx_strncpy(buffer, CHECK_NULL_EX(m_osName), bufSize);
300 }
301 else if (!_tcsicmp(param, _T("MobileDevice.OS.Version")))
302 {
303 nx_strncpy(buffer, CHECK_NULL_EX(m_osVersion), bufSize);
304 }
305 else if (!_tcsicmp(param, _T("MobileDevice.SerialNumber")))
306 {
307 nx_strncpy(buffer, CHECK_NULL_EX(m_serialNumber), bufSize);
308 }
309 else if (!_tcsicmp(param, _T("MobileDevice.Vendor")))
310 {
311 nx_strncpy(buffer, CHECK_NULL_EX(m_vendor), bufSize);
312 }
313 else if (!_tcsicmp(param, _T("MobileDevice.UserId")))
314 {
315 nx_strncpy(buffer, CHECK_NULL_EX(m_userId), bufSize);
316 }
317 else
318 {
319 rc = DCE_NOT_SUPPORTED;
320 }
321
322 return rc;
323}
26e2b253
VK
324
325/**
326 * Calculate compound status
327 */
328void MobileDevice::calculateCompoundStatus(BOOL bForcedRecalc)
329{
330 NetObj::calculateCompoundStatus(bForcedRecalc);
331
332 // Assume normal status by default for mobile device
333 if (m_iStatus == STATUS_UNKNOWN)
334 {
c42b4551 335 lockProperties();
26e2b253 336 m_iStatus = STATUS_NORMAL;
c42b4551
VK
337 setModified();
338 unlockProperties();
26e2b253
VK
339 }
340}