/* goahead.c -- Main program for GoAhead Usage: goahead [options] [documents] [IP][:port] ... Options: --auth authFile # User and role configuration --background # Run as a Linux daemon --home directory # Change to directory to run --log logFile:level # Log to file file at verbosity level --route routeFile # Route configuration file --verbose # Same as --log stdout:2 --version # Output version information Copyright (c) All Rights Reserved. See details at the end of the file. */ /********************************* Includes ***********************************/ #include "goahead.h" #include "dashboard.h" #include "cJSON.h" #include "com_BmcType.h" #include "remote_control.h" #include "fan.h" #include "fru.h" #include "sel.h" #include "server_health.h" #include "fw_update.h" #include "config.h" #include "user.h" #include #include /********************************* Defines ************************************/ static int finished = 0; UserInfo_T UserInfoTbl[MAX_USER_NUM]; /********************************* Forwards ***********************************/ static void initPlatform(); static void logHeader(); static void usage(); #if WINDOWS static void windowsClose(); static int windowsInit(); static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp); #endif #if ME_UNIX_LIKE static void sigHandler(int signo); #endif static void uploadTest(Webs *wp); /*********************************** Code *************************************/ MAIN(goahead, int argc, char **argv, char **envp) { char *argp, *home, *documents, *endpoints, *endpoint, *route, *auth, *tok, *lspec; int argind; #if WINDOWS if (windowsInit() < 0) { return 0; } #endif route = "/etc/goahead/route.txt"; auth = "/etc/goahead/auth.txt"; /**************** user code before goahead ************************/ cJSON_Hooks hooks; hooks.malloc_fn = (void *(*)(size_t))walloc; hooks.free_fn = (void (*)(void *))wfree; cJSON_InitHooks(&hooks); /**************** user code before goahead end************************/ for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } else if (smatch(argp, "--auth") || smatch(argp, "-a")) { if (argind >= argc) usage(); auth = argv[++argind]; #if ME_UNIX_LIKE && !MACOSX } else if (smatch(argp, "--background") || smatch(argp, "-b")) { websSetBackground(1); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-d") || smatch(argp, "-D")) { websSetDebug(1); } else if (smatch(argp, "--home")) { if (argind >= argc) usage(); home = argv[++argind]; if (chdir(home) < 0) { error("Cannot change directory to %s", home); exit(-1); } } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) usage(); logSetPath(argv[++argind]); } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { logSetPath("stdout:2"); //2 } else if (smatch(argp, "--route") || smatch(argp, "-r")) { route = argv[++argind]; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { printf("%s\n", ME_VERSION); exit(0); } else if (*argp == '-' && isdigit((uchar) argp[1])) { lspec = sfmt("stdout:%s", &argp[1]); logSetPath(lspec); wfree(lspec); } else { usage(); } } documents = ME_GOAHEAD_DOCUMENTS; if (argc > argind) { documents = argv[argind++]; } printf("---> initPlatform\n"); initPlatform(); printf("---> websOpen\n"); // printf(">>>>>>>>>>>>>>>>>>>>>>>>>>2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); if (websOpen(documents, route) < 0) { error("Cannot initialize server. Exiting."); return -1; } #if ME_GOAHEAD_AUTH printf("---> websLoad\n"); if (websLoad(auth) < 0) { error("Cannot load %s", auth); return -1; } #endif logHeader(); if (argind < argc) { while (argind < argc) { endpoint = argv[argind++]; printf("---> websListen log1\n"); if (websListen(endpoint) < 0) { return -1; } } } else { endpoints = sclone(ME_GOAHEAD_LISTEN); for (endpoint = stok(endpoints, ", \t", &tok); endpoint; endpoint = stok(NULL, ", \t,", &tok)) { #if !ME_COM_SSL if (strstr(endpoint, "https")) continue; #endif printf("---> websListen log2\n"); if (websListen(endpoint) < 0) { wfree(endpoints); return -1; } } wfree(endpoints); } #if ME_ROM && KEEP /* If not using a route/auth config files, then manually create the routes like this: If custom matching is required, use websSetRouteMatch. If authentication is required, use websSetRouteAuth. */ websAddRoute("/", "file", 0); #endif /**************** user code after goahead ************************/ websDefineAction("test", actionTest); websDefineAction("buy", buy); //dashboard websDefineAction("getDeviceInfo", getDeviceInfo); websDefineAction("getSysInfo", getSysInfo); websDefineAction("getCardInfo", getCardInfo); websDefineAction("getSensorInfo", getSensorInfo); //remote control websDefineAction("chassisPwrCtrl", chassisPwrCtrl); websDefineAction("getChassisStatus", getChassisStatus); //Fru //websDefineAction("getFruInfo", getFruInfo); websDefineAction("getFruChassisInfo", getFruChassisInfo); websDefineAction("getFruBoardInfo", getFruBoardInfo); websDefineAction("getFruProductInfo", getFruProductInfo); //sel websDefineAction("Web_ClearSEL", Web_ClearSEL); websDefineAction("GetAllSELEntriesSorted", GetAllSELEntriesSorted); //user websDefineAction("getAllUserInfo", getAllUserInfo); websDefineAction("setUserPassword", setUserPassword); websDefineAction("addUser", addUser); websDefineAction("delUser", delUser); //update firmware websDefineAction("uploadTest", uploadTest); websDefineAction("prepareDevice", prepareDevice); websDefineAction("uploadFirmware", uploadFirmware); websDefineAction("updateFlash", updateFlash); websDefineAction("getUpdateProgress", getUpdateProgress); websDefineAction("getVerifyStatus", getVerifyStatus); websDefineAction("resetBmc", resetBmc); /**************** user code after goahead end************************/ #if ME_UNIX_LIKE && !MACOSX /* Service events till terminated */ if (websGetBackground()) { printf("---> daemon\n"); if (daemon(0, 0) < 0) { error("Cannot run as daemon"); return -1; } } #endif printf("---> websServiceEvents\n"); websServiceEvents(&finished); logmsg(1, "Instructed to exit"); printf("---> websClose\n"); websClose(); #if WINDOWS windowsClose(); #endif return 0; } // typedef struct{ // char buf[1024*1000]; // char filename[32]; // int fileSize; // } filestruct; // //这个函数是把16进制字符串转换成16进制 // int StringToHex(char *str, unsigned char *out, unsigned int *outlen) // { // char *p = str; // char high = 0, low = 0; // int tmplen = strlen(p), cnt = 0; // tmplen = strlen(p); // while(cnt < (tmplen / 2)) // { // high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48; // low = (*(++ p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p) - 48 - 7 : *(p) - 48; // out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f)); // p ++; // cnt ++; // } // if(tmplen % 2 != 0) out[cnt] = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48; // if(outlen != NULL) *outlen = tmplen / 2 + tmplen % 2; // return tmplen / 2 + tmplen % 2; // } static void uploadTest(Webs *wp) { WebsKey *s; WebsUpload *up; char *upfile; websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteHeader(wp, "Content-Type", "text/plain"); websWriteEndHeaders(wp); if (scaselessmatch(wp->method, "POST")) { for (s = hashFirst(wp->files); s; s = hashNext(wp->files, s)) { up = s->content.value.symbol; websWrite(wp, "FILE: %s\r\n", s->name.value.string); websWrite(wp, "FILENAME=%s\r\n", up->filename); websWrite(wp, "CLIENT=%s\r\n", up->clientFilename); websWrite(wp, "TYPE=%s\r\n", up->contentType); websWrite(wp, "SIZE=%d\r\n", up->size); upfile = sfmt("%s/tmp/%s", websGetDocuments(), up->clientFilename); printf("<<<<<<<<<<<%s>>>>>>>>>>\n", upfile); if (rename(up->filename, upfile) < 0) { error("Cannot rename uploaded file: %s to %s, errno %d", up->filename, upfile, errno); } wfree(upfile); } websWrite(wp, "\r\nVARS:\r\n"); for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) { websWrite(wp, "%s=%s\r\n", s->name.value.string, s->content.value.string); } } // char *pStr; // cJSON * root = cJSON_CreateObject(); // cJSON * data = cJSON_CreateObject(); // cJSON_AddItemToObject(root, "data", data);//根节点下添加 // cJSON_AddStringToObject(root, "msg", ""); // cJSON_AddNumberToObject(root, "code", 200); // pStr = cJSON_PrintUnformatted(root); // printf("---> cJSON Str:\n%s\n", pStr); // websSetStatus(wp, 200); // websWriteHeaders(wp, -1, 0); // websWriteEndHeaders(wp); // websWrite(wp,"%s", pStr); // websFlush(wp, 0); // // websDone(wp); // if(pStr) // wfree(pStr); // if(root) // cJSON_Delete(root); websDone(wp); } static void logHeader() { char home[ME_GOAHEAD_LIMIT_STRING]; getcwd(home, sizeof(home)); logmsg(2, "Configuration for %s", ME_TITLE); logmsg(2, "---------------------------------------------"); logmsg(2, "Version: %s", ME_VERSION); logmsg(2, "BuildType: %s", ME_DEBUG ? "Debug" : "Release"); logmsg(2, "CPU: %s", ME_CPU); logmsg(2, "OS: %s", ME_OS); logmsg(2, "Host: %s", websGetServer()); logmsg(2, "Directory: %s", home); logmsg(2, "Documents: %s", websGetDocuments()); logmsg(2, "Configure: %s", ME_CONFIG_CMD); logmsg(2, "---------------------------------------------"); } static void usage() { fprintf(stderr, "\n%s Usage:\n\n" " %s [options] [documents] [[IPaddress][:port] ...]\n\n" " Options:\n" #if ME_GOAHEAD_AUTH " --auth authFile # User and role configuration\n" #endif #if ME_UNIX_LIKE && !MACOSX " --background # Run as a Unix daemon\n" #endif " --debugger # Run in debug mode\n" " --home directory # Change to directory to run\n" " --log logFile:level # Log to file file at verbosity level\n" " --route routeFile # Route configuration file\n" " --verbose # Same as --log stdout:2\n" " --version # Output version information\n\n", ME_TITLE, ME_NAME); exit(-1); } static void initPlatform() { #if ME_UNIX_LIKE signal(SIGTERM, sigHandler); #ifdef SIGPIPE signal(SIGPIPE, SIG_IGN); #endif #elif ME_WIN_LIKE _fmode=_O_BINARY; #endif } #if ME_UNIX_LIKE static void sigHandler(int signo) { finished = 1; } #endif #if WINDOWS /* Create a taskbar entry. Register the window class and create a window */ static int windowsInit() { HINSTANCE inst; WNDCLASS wc; /* Window class */ HMENU hSysMenu; HWND hwnd; inst = websGetInst(); wc.style = CS_HREDRAW | CS_VREDRAW; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = inst; wc.hIcon = NULL; wc.lpfnWndProc = (WNDPROC) websWindProc; wc.lpszMenuName = wc.lpszClassName = ME_NAME; if (! RegisterClass(&wc)) { return -1; } /* Create a window just so we can have a taskbar to close this web server */ hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT, 0, 0, 0, NULL, NULL, inst, NULL); if (hwnd == NULL) { return -1; } /* Add the about box menu item to the system menu */ hSysMenu = GetSystemMenu(hwnd, FALSE); if (hSysMenu != NULL) { AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL); } ShowWindow(hwnd, SW_SHOWNORMAL); UpdateWindow(hwnd); return 0; } static void windowsClose() { HINSTANCE inst; inst = websGetInst(); UnregisterClass(ME_NAME, inst); } /* Main menu window message handler. */ static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); finished++; return 0; case WM_SYSCOMMAND: break; } return DefWindowProc(hwnd, msg, wp, lp); } /* Check for Windows Messages */ WPARAM checkWindowsMsgLoop() { MSG msg; if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) { return msg.wParam; } TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } /* Windows message handler */ static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp) { LRESULT lResult; lResult = DefWindowProc(hwndDlg, msg, wp, lp); switch (msg) { case WM_CREATE: break; case WM_DESTROY: break; case WM_COMMAND: break; } return lResult; } #endif