⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 startup.c

📁 Open DMT Client C Source code
💻 C
📖 第 1 页 / 共 3 页
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2007, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description://  Main entry point and custom startup initialization//  Provides customized startup initialization.// ---// Change History://  2006/01/04  Martin D. Flynn//     -Initial release//  2006/01/12  Martin D. Flynn//     -Integrated syslog support//  2006/01/27  Martin D. Flynn//     -Changed '-debug' to send output logs to console//     -Fixed bug where "TRANSPORT_MEDIA_SOCK" should have been "TRANSPORT_MEDIA_SOCKET"//  2006/02/12  Martin D. Flynn//     -Changed to accomodate new return type from "gpsGetDiagnostics".//     -Changed to save properties back to 'propertyCache', instead of 'propertyFile'.//  2006/04/10  Martin D. Flynn//     -Changed Socket property 'PROP_MOTION_IN_MOTION' default to 15 minutes.//  2006/06/07  Martin D. Flynn//     -Relaxed TRANSPORT_MEDIA_SOCKET connection settings//  2007/01/28  Martin D. Flynn//     -Many changes to facilitate WindowsCE port//  2007/04/28  Martin D. FLynn//     -Don't queue events if either PROP_COMM_HOST or PROP_COMM_PORT are undefined.// ----------------------------------------------------------------------------#include "stdafx.h" // TARGET_WINCE#include "custom/defaults.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include "custom/log.h"#include "custom/gps.h"#include "custom/gpsmods.h"#include "custom/startup.h"#include "custom/transport.h"#include "modules/motion.h"#include "modules/odometer.h"#if defined(ENABLE_GEOZONE)#  include "modules/geozone.h"#endif#include "tools/stdtypes.h"#include "tools/utctools.h"#include "tools/strtools.h"#include "tools/bintools.h"#include "tools/threads.h"#include "tools/io.h"#include "tools/comport.h"#include "base/cerrors.h"#include "base/propman.h"#include "base/statcode.h"#include "base/mainloop.h"#include "base/accting.h"#include "base/events.h"#include "base/protocol.h"#if defined(ENBALE_UPLOAD)#  include "base/upload.h"#endif#if defined(TARGET_WINCE)#  include <aygshell.h>#  include "custom/wince/wceos.h"#else// TODO: should separate this into gumstix/linux/cygwin#  include "custom/linux/os.h"#endif// ----------------------------------------------------------------------------// Version string// Examples://   OpenDMTP_FILE.1.0.3//   OpenDMTP_SOCK.1.0.3#define DMTP_NAME                   APPLICATION_NAME#if   defined(TRANSPORT_MEDIA_SOCKET)#  define DMTP_TYPE                 "SOCK"  // - Socket communication#elif defined(TRANSPORT_MEDIA_SERIAL)#  define DMTP_TYPE                 "SER"   // - BlueTooth communication#elif defined(TRANSPORT_MEDIA_GPRS)#  define DMTP_TYPE                 "GPRS"  // - GPRS communication#elif defined(TRANSPORT_MEDIA_FILE)#  define DMTP_TYPE                 "FILE"  // - File event capture#endif#if !defined(DMTP_TYPE)#  error DMTP_TYPE not defined.#endif#define DMTP_NAME_TYPE_VERSION      DMTP_NAME "_" DMTP_TYPE "." RELEASE_VERSIONconst char APP_VERSION[] = { "$$VERSION:" DMTP_NAME_TYPE_VERSION "[" APPLICATION_FEATURES "]" };// ----------------------------------------------------------------------------// GPRS and SOCKET connect host:port#define DEFAULT_COMM_HOST           "localhost"#define DEFAULT_COMM_PORT           "31000" // this default may change at any time// ----------------------------------------------------------------------------#if defined(PROPERTY_FILE)/* property file names */static char propertyFile[80]  = { PROPERTY_FILE  };static char propertyCache[90] = { PROPERTY_CACHE };#if defined(PROPERTY_SAVE_INTERVAL)#define FIRST_PROPERTY_SAVE_INTERVAL    20Lstatic TimerSec_t   lastSavePropertyTimer = 0L;static UInt32       lastSavePropertyInterval = FIRST_PROPERTY_SAVE_INTERVAL;#endif#endif/* server host:port defined */#if defined(TRANSPORT_MEDIA_SOCKET) || defined(TRANSPORT_MEDIA_GPRS)static utBool hasServerHostPort = utTrue;#endif// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------// define CUSTOM_EVENT_PACKETS to enable custom event packet//#define CUSTOM_EVENT_PACKETS#if defined(CUSTOM_EVENT_PACKETS) // {// Custom defined event packets#endif // } defined(CUSTOM_EVENT_PACKETS)// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------/* add the specified event to the queue */static utBool _customAddSingleEvent(PacketPriority_t priority, ClientPacketType_t pktType, Event_t *er){    /* no event? */    if (!er) {        return utFalse;    }    /* add event packet */    Packet_t pkt;    utBool didAdd = evAddEventPacket(&pkt, priority, pktType, er);    UInt32 pktSeq = pkt.sequence;    /* display event */    logDEBUG(LOGSRC,"$%04lX:%lu,%04X,%.4lf/%.4lf:%ld,%.1lf,%s,%s,%04lX",         pktType, er->timestamp[0], er->statusCode,         er->gpsPoint[0].latitude, er->gpsPoint[0].longitude, er->gpsQuality,         er->odometerKM,         er->entity[0], er->entity[1],        pktSeq);    /* return success */    return didAdd;}/* add the specified event to the queue */static utBool _customAddEventFtn(PacketPriority_t priority, ClientPacketType_t pktType, Event_t *er){    /* no event? */    if (!er) {        return utFalse;    }        /* special event handling */    // perform any special event handling here    /* skip out if priority is PRIORITY_NONE */    if (priority == PRIORITY_NONE) {        // normally this will never occur, however, the client may choose to set the        // priority on a special event to PRIORITY_NONE in order to be able to handle        // it above and discard it here if necessary.        return utFalse;    }    /* skip if the packet type is not a standard event packet */    if (!pktIsEventPacket(pktType)) {        // generally, this will only occur in an HOS environment        return utFalse; // not a vehicle telemetry event    }    /* don't queue events if we don't have anywhere to send the data */#if defined(TRANSPORT_MEDIA_SOCKET) || defined(TRANSPORT_MEDIA_GPRS)    if (!hasServerHostPort) {        return utFalse; // host:port not defined, don't queue event    }#endif // defined(TRANSPORT_MEDIA_SOCKET) || defined(TRANSPORT_MEDIA_GPRS)    /* promote remaining priorities to PRIORITY_HIGH for file and serial transport */#if defined(TRANSPORT_MEDIA_FILE) || defined(TRANSPORT_MEDIA_SERIAL)    priority = PRIORITY_HIGH;#endif    /* add single event */    return _customAddSingleEvent(priority, pktType, er);}// ----------------------------------------------------------------------------/* property refresh, prior to a 'Get' */static void _propertyPreGET(PropertyRefresh_t mode, Key_t key, UInt8 *args, int argLen){    if (mode & PROP_REFRESH_GET) {        switch (key) {            case PROP_STATE_TIME: {                // update property with clock time                propSetUInt32(PROP_STATE_TIME, utcGetTimeSec());            } break;            case PROP_STATE_GPS: {                // update property with current GPS location                GPS_t lastGPS;                GPSOdometer_t gpsOdom;                memcpy((GPSShort_t*)&gpsOdom, (GPSShort_t*)gpsGetLastGPS(&lastGPS,-1), sizeof(GPSShort_t));                gpsOdom.meters = (UInt32)(odomGetDeviceDistanceMeters() + 0.5); // ROUND(X?)                propSetGPS(PROP_STATE_GPS, &gpsOdom);            } break;            case PROP_STATE_GPS_DIAGNOSTIC: {                // return GPS diagnostics (ie. GPS module health)                int i;                UInt32 *gpsStats = (UInt32*)gpsGetDiagnostics((GPSDiagnostics_t*)0);                for (i = 0; i < (sizeof(GPSDiagnostics_t)/sizeof(UInt32)); i++) {                    propSetUInt32AtIndex(PROP_STATE_GPS_DIAGNOSTIC, i, gpsStats[i]);                }            } break;            case PROP_STATE_QUEUED_EVENTS: {                // update property with number of events                Int32 evQueCnt = 0L, evTotCnt = 0L;                evQueCnt = evGetPacketCount();                evTotCnt = evGetTotalPacketCount();                propSetUInt32AtIndex(PROP_STATE_QUEUED_EVENTS, 0, (UInt32)evQueCnt);                propSetUInt32AtIndex(PROP_STATE_QUEUED_EVENTS, 1, (UInt32)evTotCnt);            } break;            case PROP_GEOF_COUNT: {                // update property with number of GeoZone entries#if defined(ENABLE_GEOZONE)                propSetUInt32(PROP_GEOF_COUNT, (UInt32)geozGetGeoZoneCount());#endif            } break;        }    } }/* property update, after a 'Set' */static void _propertyPostSET(PropertyRefresh_t mode, Key_t key, UInt8 *args, int argLen){    if (mode & PROP_REFRESH_SET) {        switch (key) {            case PROP_STATE_DEVICE_ID: {                // change host/device name                const char *s = propGetDeviceID(0); // propGetString(PROP_STATE_DEVICE_ID,"");                if (!s || !*s) {                     s = DEVICE_ID;                     if (!s || !*s) {                        s = propGetString(PROP_STATE_SERIAL, "");                    }                }#if !defined(SECONDARY_SERIAL_TRANSPORT)                // set bluetooth broadcast name                osSetHostname(s);#endif                // save properties to save new ID?            } break;#if defined(SECONDARY_SERIAL_TRANSPORT)            case PROP_STATE_DEVICE_BT: {                // change bluetooth broadcast name                const char *s = propGetDeviceID(1); // propGetString(PROP_STATE_DEVICE_BT,"");                if (!s || !*s) {                     s = DEVICE_ID;                     if (!s || !*s) {                        s = propGetString(PROP_STATE_SERIAL, "");                    }                }                // set bluetooth broadcast name                osSetHostname(s);                // save properties to save new ID?            } break;#endif        }    }}// ----------------------------------------------------------------------------/* status "ping" */CommandError_t startupPingStatus(PacketPriority_t priority, StatusCode_t code, int ndx){    ClientPacketType_t pktType = DEFAULT_EVENT_FORMAT;        /* initialize an event in anticipation that the status code is valid */    GPS_t gps;    Event_t evRcd;    evSetEventDefaults(&evRcd, code, 0L, gpsGetLastGPS(&gps,-1));    /* check specified status code */    if ((code == STATUS_LOCATION) || (code == STATUS_WAYMARK) || (code == STATUS_QUERY)) {        if (ndx > 0) {            // index was specified and was greater than '0'            return COMMAND_INDEX;        } else {            // send current location            _customAddEventFtn(priority, pktType, &evRcd);            return COMMAND_OK;        }    } else    if ((code >= STATUS_ODOM_0) && (code <= STATUS_ODOM_7)) {        int odoNdx = code - STATUS_ODOM_0; // odometer index        if ((ndx >= 0) && (ndx != odoNdx)) {            // index was specified, but is not equal to the odomenter index            return COMMAND_INDEX;        } else {            // send current odometer value            // 'evRcd.odometerKM' is already be set above (in 'evSetEventDefaults')            if (odoNdx == 0) {                // vehicle distance (may need special handling                evRcd.distanceKM = odomGetDeviceDistanceMeters() / 1000.0;            } else {                // driver, etc                evRcd.distanceKM = odomGetDistanceMetersAtIndex(odoNdx) / 1000.0;            }            _customAddEventFtn(priority, pktType, &evRcd);            return COMMAND_OK;        }    } else {        // Other candidated:        //  STATUS_ELAPSED_xx        return COMMAND_STATUS;    }    }// ----------------------------------------------------------------------------/* status "ping" [see PROP_CMD_STATUS_EVENT] */static CommandError_t _cmdSendStatus(int protoNdx, Key_t key, const UInt8 *data, int dataLen){    // 'protoNdx' contains the handle to the protocol transport    /* parse arguments */    UInt32 code32 = 0L;    UInt32 ndx32  = 0L;    int flds = binScanf(data, dataLen, "%2u%1u", &code32, &ndx32);    if (flds < 1) {        return COMMAND_ARGUMENTS;    }    StatusCode_t code = (StatusCode_t)code32;    int ndx = (flds >= 2)? (int)ndx32 : -1;        /* status ping */    return startupPingStatus(PRIORITY_HIGH, code, ndx);     }/* set digital output [see PROP_CMD_SET_OUTPUT] */static CommandError_t _cmdSetOutput(int protoNdx, Key_t key, const UInt8 *data, int dataLen){    // 'protoNdx' contains the handle to the protocol transport    /* parse arguments */    UInt32 ndx    = 0L;    UInt32 state  = 0L;    UInt32 duraMS = 0L;    int flds = binScanf(data, dataLen, "%1u%1u%4u", &ndx, &state, &duraMS);    if (flds < 2) {        return COMMAND_ARGUMENTS;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -