00001 /* ----------------------------------------------------------------------------- 00002 * function.c 00003 * 00004 * PMU Simulator - Phasor Measurement Unit Simulator 00005 * 00006 * Copyright (C) 2011 Nitesh Pandit 00007 * Copyright (C) 2011 Kedar V. Khandeparkar 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 * 00023 * Authors: 00024 * Kedar V. Khandeparkar <kedar.khandeparkar@gmail.com> 00025 * Nitesh Pandit <panditnitesh@gmail.com> 00026 * 00027 * ----------------------------------------------------------------------------- */ 00028 00029 00030 /* ------------------------------------------------------------------------------------ */ 00031 /* Functions defined in function.c */ 00032 /* ------------------------------------------------------------------------------------ */ 00033 00034 /* 1. void B_copy(unsigned char main[],unsigned char tmp[],int ind,int n) */ 00035 /* 2. void stn_name (char temp_5[]) */ 00036 /* 3. void H2S(char a[], unsigned char temp_6[]) */ 00037 /* 4. void i2c (int t, unsigned char temp[]) */ 00038 /* 5. void li2c (long int t1, unsigned char temp_1[]) */ 00039 /* 6. void f2c (float f, unsigned char temp_4[]) */ 00040 /* 7. int c2i (unsigned char temp_2[]) */ 00041 /* 8. long int c2li (unsigned char temp_3[]) */ 00042 /* 9. uint16_t compute_CRC(unsigned char *message,char length) */ 00043 /* 10. void sigchld_handler(int s) */ 00044 /* 11. int terminal(void) */ 00045 00046 /* ------------------------------------------------------------------------------------ */ 00047 00048 #include <stdio.h> 00049 #include <string.h> 00050 #include <stdlib.h> 00051 #include <stdint.h> 00052 #include <dirent.h> 00053 #include <sys/wait.h> 00054 #include "function.h" 00055 00056 #define channel_name 100 00057 00058 00059 /* ---------------------------------------------------------------------------- */ 00060 /* FUNCTION B_copy(unsigned char main[], unsigned char tmp[], int ind, int n): */ 00061 /* Function copies Bytes in a main array */ 00062 /* ---------------------------------------------------------------------------- */ 00063 00064 void B_copy(unsigned char main[], unsigned char tmp[], int ind, int n) 00065 { 00066 int k; 00067 for(k=0; k<n; k++) 00068 { 00069 main[ind+k] = tmp[k]; 00070 } 00071 }; 00072 00073 00074 /* ---------------------------------------------------------------------------- */ 00075 /* FUNCTION H2S(char a[], unsigned char temp_6[]): */ 00076 /* Function for Hexa to String Conversion */ 00077 /* ---------------------------------------------------------------------------- */ 00078 00079 void H2S(char a[], unsigned char temp_6[]) 00080 { 00081 int k; 00082 00083 for(k=0; k<16; k++) 00084 { 00085 a[k] = temp_6[k]; 00086 } 00087 a[16] = '\0'; 00088 } 00089 00090 00091 /* ---------------------------------------------------------------------------- */ 00092 /* FUNCTION i2c (int t, unsigned char temp[]): */ 00093 /* Function for Integer to Character Conversion */ 00094 /* ---------------------------------------------------------------------------- */ 00095 00096 void i2c (int t, unsigned char temp[]) 00097 { 00098 00099 temp[0] = t>>8; 00100 temp[1] = t; 00101 } 00102 00103 00104 /* ---------------------------------------------------------------------------- */ 00105 /* FUNCTION li2c (long int t1, unsigned char temp_1[]): */ 00106 /* Function for Long Integer to Character Conversion */ 00107 /* ---------------------------------------------------------------------------- */ 00108 00109 void li2c (long int t1, unsigned char temp_1[]) 00110 { 00111 00112 temp_1[0] = t1>>24; 00113 temp_1[1] = t1>>16; 00114 temp_1[2] = t1>>8; 00115 temp_1[3] = t1; 00116 } 00117 00118 00119 /* ---------------------------------------------------------------------------- */ 00120 /* FUNCTION li2c (long int t1, unsigned char temp_1[]): */ 00121 /* Function for float to Character Conversion */ 00122 /* ---------------------------------------------------------------------------- */ 00123 00124 void f2c (float f, unsigned char temp_1[]) 00125 { 00126 int i, j; 00127 float fv; 00128 unsigned char a1[sizeof fv]; 00129 00130 fv = f; 00131 memcpy(a1, &fv, sizeof fv); 00132 for (i=0, j=3; i<sizeof fv; i++, j--) 00133 { 00134 temp_1[j] = a1[i]; 00135 } 00136 } 00137 00138 00139 /* ---------------------------------------------------------------------------- */ 00140 /* FUNCTION c2i (unsigned char temp[]): */ 00141 /* Function for Character to Integer Conversion */ 00142 /* ---------------------------------------------------------------------------- */ 00143 00144 int c2i (unsigned char temp[]) 00145 { 00146 int i; 00147 00148 i = temp[0]; 00149 i<<=8; 00150 i |=temp[1]; 00151 return(i); 00152 } 00153 00154 00155 /* ---------------------------------------------------------------------------- */ 00156 /* FUNCTION c2li (unsigned char temp_3[]): */ 00157 /* Function for Character to Long Integer Conversion */ 00158 /* ---------------------------------------------------------------------------- */ 00159 00160 long int c2li (unsigned char temp_3[]) 00161 { 00162 long int i; 00163 unsigned char a[4]; 00164 memset(a, '\0', 4); 00165 strcpy((char *)a, (char *)temp_3); 00166 00167 i = a[0]; 00168 i<<=8; 00169 i |=a[1]; 00170 i<<=8; 00171 i |=a[2]; 00172 i<<=8; 00173 i |=a[3]; 00174 return(i); 00175 } 00176 00177 00178 /* ---------------------------------------------------------------------------- */ 00179 /* FUNCTION uint16_t compute_CRC(unsigned char *message,char length): */ 00180 /* Function for calculation of CHECKSUM CRC-CCITT (0xffff) */ 00181 /* ---------------------------------------------------------------------------- */ 00182 00183 uint16_t compute_CRC(unsigned char *message,char length) 00184 { 00185 uint16_t crc=0x0ffff,temp,quick; 00186 int i; 00187 00188 for(i=0;i<length;i++) 00189 { 00190 temp=(crc>>8)^message[i]; 00191 crc<<=8; 00192 quick=temp ^ ( temp >>4); 00193 crc ^=quick; 00194 quick<<=5; 00195 crc ^=quick; 00196 quick <<=7; 00197 crc ^= quick; 00198 } 00199 return crc; 00200 } 00201 00202 00203 /* ---------------------------------------------------------------------------- */ 00204 /* FUNCTION sigchld_handler(int s): */ 00205 /* Function for TCP connection signal handling */ 00206 /* ---------------------------------------------------------------------------- */ 00207 00208 void sigchld_handler(int s) 00209 { 00210 while(wait(NULL) > 0); 00211 } 00212 00213 /*************************************************************** End of File *********************************************************************************/