831 struct timespec timeout;
834 pthread_t thread_handle;
836 IssueStruct issueParam;
837 CommandHeader header;
838 char* pHeaderStream = 0;
839 MemoryNode* memNode = 0;
847 "FeeServer CommandHandler (Received command) - Packet-ID: %d",
858 issueParam.nRet = FEE_UNKNOWN_RETVAL;
861 if ((state != RUNNING) && (state != ERROR_STATE)) {
870 printf(
"Lock command mutex error: %d\n", status);
876 if ((tag == 0) || (address == 0) || (
size == 0)) {
878 "Received null pointer of DIM framework in command handler.");
882 if (*
size < HEADER_SIZE) {
884 "FeeServer received corrupted command.");
889 printf(
" Cmnd - Size: %d\n", *
size);
894 memcpy(&header.id, address, HEADER_SIZE_ID);
895 memcpy(&header.errorCode, address + HEADER_OFFSET_ID, HEADER_SIZE_ERROR_CODE);
896 memcpy(&header.flags, address + HEADER_OFFSET_ERROR_CODE, HEADER_SIZE_FLAGS);
897 memcpy(&header.checksum, address + HEADER_OFFSET_FLAGS, HEADER_SIZE_CHECKSUM);
900 if ((header.flags & HUFFMAN_FLAG) != 0) {
905 issueParam.size = *
size - HEADER_SIZE;
906 issueParam.command = (address + HEADER_SIZE);
909 if ((header.flags & CHECKSUM_FLAG) != 0) {
911 if (!
checkCommand(issueParam.command, issueParam.size, header.checksum)) {
914 "FeeServer received corrupted command data (checksum failed).");
920 if ((header.flags & FEESERVER_UPDATE_FLAG) != 0) {
921 #ifdef ENABLE_MASTERMODE 924 createLogMessage(MSG_WARNING,
"FeeServer is not authorized to execute shell programs, skip ...", 0);
925 #endif //ENABLE_MASTERMODE 927 issueParam.nRet = FEE_FAILED;
929 }
else if ((header.flags & FEESERVER_RESTART_FLAG) != 0) {
931 }
else if ((header.flags & FEESERVER_REBOOT_FLAG) != 0) {
935 }
else if ((header.flags & FEESERVER_SHUTDOWN_FLAG) != 0) {
939 }
else if ((header.flags & FEESERVER_EXIT_FLAG) != 0) {
941 }
else if ((header.flags & FEESERVER_SET_DEADBAND_FLAG) != 0) {
943 }
else if ((header.flags & FEESERVER_GET_DEADBAND_FLAG) != 0) {
945 }
else if ((header.flags & FEESERVER_SET_ISSUE_TIMEOUT_FLAG) != 0) {
947 }
else if ((header.flags & FEESERVER_GET_ISSUE_TIMEOUT_FLAG) != 0) {
949 }
else if ((header.flags & FEESERVER_SET_UPDATERATE_FLAG) != 0) {
951 }
else if ((header.flags & FEESERVER_GET_UPDATERATE_FLAG) != 0) {
953 }
else if ((header.flags & FEESERVER_SET_LOGLEVEL_FLAG) != 0) {
955 }
else if ((header.flags & FEESERVER_GET_LOGLEVEL_FLAG) != 0) {
959 if (state == ERROR_STATE) {
961 "FeeServer is in ERROR_STATE, ignoring command for CE!");
966 if (issueParam.size == 0) {
968 "FeeServer received empty command.");
973 status = pthread_mutex_lock(&
wait_mut);
976 "Unable to lock condition mutex for watchdog.");
980 status = pthread_attr_init(&attr);
984 "Unable to initialize issue thread.");
988 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
992 "Unable to initialize issue thread.");
996 status = pthread_create(&thread_handle, &attr, &
threadIssue, (
void*) &issueParam);
1000 "Unable to create issue thread.");
1004 status = pthread_attr_destroy(&attr);
1007 printf(
"Destroy attribute error: %d\n", status);
1011 "Unable to destroy thread attribute.", 0);
1015 status = gettimeofday(&now, 0);
1019 timeout.tv_sec = now.tv_sec + (int) (
issueTimeout / 1000);
1022 timeout.tv_nsec = (now.tv_usec * 1000) +
1028 retcode = pthread_cond_timedwait(&
cond, &
wait_mut, &timeout);
1030 printf(
"Retcode of CMND timedwait: %d\n", retcode);
1035 if (retcode == ETIMEDOUT) {
1037 printf(
"ControlEngine watchdog detected TimeOut.\n");
1041 "ControlEngine watch dog noticed a time out for last command.", 0);
1044 pthread_cancel(thread_handle);
1046 issueParam.nRet = FEE_TIMEOUT;
1047 issueParam.size = 0;
1048 }
else if (retcode != 0) {
1051 printf(
"ControlEngine watchdog detected unknown error.\n");
1055 "ControlEngine watch dog received an unknown for last command.", 0);
1058 pthread_cancel(thread_handle);
1060 issueParam.nRet = FEE_THREAD_ERROR;
1061 issueParam.size = 0;
1066 printf(
"Get time of day error: %d\n", status);
1070 "Watchdog timer could not be initialized. Using non-reliable sleep instead.",
1077 status = pthread_cancel(thread_handle);
1081 printf(
"TimeOut occured.\n");
1084 "ControlEngine issue did not return in time.", 0);
1085 issueParam.nRet = FEE_TIMEOUT;
1086 issueParam.size = 0;
1096 printf(
"Issue-nRet: %d\n", issueParam.nRet);
1100 if ((issueParam.nRet < FEE_UNKNOWN_RETVAL) ||
1101 (issueParam.nRet > FEE_MAX_RETVAL)) {
1102 issueParam.nRet = FEE_UNKNOWN_RETVAL;
1104 "ControlEngine [command] returned unkown RetVal.", 0);
1134 printf(
"no memory available!\n");
1144 printf(
"Lock command mutex error: %d\n", status);
1148 "Error while trying to unlock command mutex.", 0);
1154 if (issueParam.size > 0) {
1162 if ((header.flags & CHECKSUM_FLAG) != 0) {
1167 header.checksum = CHECKSUM_ZERO;
1171 header.errorCode = (short) issueParam.nRet;
1173 printf(
"ErrorCode in Header: %d\n", header.errorCode);
1178 memcpy((
void*)
cmndACK, (
void*) pHeaderStream, HEADER_SIZE);
1179 if (pHeaderStream != 0) {
1180 free(pHeaderStream);
1193 memcpy(((
void*)
cmndACK + HEADER_SIZE), (
void*) issueParam.result,
1210 if ((!useMM) && (issueParam.result != 0)) {
1211 free(issueParam.result);
1302 printf(
"Lock command mutex error: %d\n", status);
1306 "Error while trying to unlock command mutex.", 0);
unsigned int calculateChecksum(unsigned char *buffer, int size)
int setUpdateRate(IssueStruct *issueParam)
void fee_exit_handler(unsigned int state)
void initIssueStruct(IssueStruct *issueStr)
int setLogLevel(IssueStruct *issueParam)
static unsigned int serviceACKID
MemoryNode * findMemoryNode(void *addr)
void updateFeeServer(IssueStruct *issueParam)
void createLogMessage(unsigned int type, char *description, char *origin)
int getUpdateRate(IssueStruct *issueParam)
static pthread_mutex_t wait_mut
static pthread_mutex_t command_mut
#define dis_update_service
int setIssueTimeout(IssueStruct *issueParam)
static pthread_cond_t cond
int getDeadband(IssueStruct *issueParam)
void freeMemoryNode(MemoryNode *node)
int setDeadband(IssueStruct *issueParam)
void createBenchmark(char *msg)
void leaveCommandHandler(unsigned int id, short errorCode, unsigned int msgType, char *message)
static unsigned long issueTimeout
void * threadIssue(void *threadParam)
bool checkCommand(char *payload, int size, unsigned int checksum)
sprintf(name1,"NewService%d", i)
char * marshallHeader(CommandHeader *pHeader)
int getIssueTimeout(IssueStruct *issueParam)
int getLogLevel(IssueStruct *issueParam)