runnams.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 812 行 · 第 1/2 页

C
812
字号
        printf(" changed state - (%d) \"", pEventData->state);        switch (pEventData->state) {            case MIDP_MIDLET_STATE_ACTIVE: printf("ACTIVE\""); break;            case MIDP_MIDLET_STATE_PAUSED: printf("PAUSED\""); break;            case MIDP_MIDLET_STATE_DESTROYED:                printf("DESTROYED\"");                if (pEventData->reason == MIDP_REASON_TERMINATED) {                    printf("Suite has terminated. ID = %ld\n", (long)suiteId);                }                break;            case MIDP_MIDLET_STATE_ERROR: printf("ERROR\""); break;            default: printf("INVALID!!!\""); break;        }        printf("  reason = %d\n", pEventData->reason);    }}#if ENABLE_I3_TESTstatic void initNamsCommands(int argn, char* args[]) {    int i;    int* cmd;    if (argn <= 0) {        return;    }    cmd = midpMalloc(sizeof(int) * (1 + 2 * (argn - 1)));    cmd[0] = argn - 1;    for (i = 1; i < argn; ++i) {        cmd[1 + 2 * (i - 1) + 0] = 6;        cmd[1 + 2 * (i - 1) + 1] = atoi(args[i]);    };    for (i = 1; i < (1 + 2 * (argn - 1)); i+=2) {        /*        printf("DEBUG: midlet starter: cmd = %i, param = %i\n",            cmd[i+0], cmd[i+1]);        */        if (cmd[i + 0] != 0 && cmd[i + 1] >= 0) {            midp_startNativeThread(                (midp_ThreadRoutine*)&midlet_starter_routine,                (midp_ThreadRoutineParameter)&cmd[i]);        }    }    /*     * Now cmd is not destroyed my midpFree(cmd) -     * this storage is needed by spawned threads after this routine returns...     */}#endif/** * Sets up the arguments required to start a midlet: * suiteIDToRun, classNameToRun, argsForMidlet[] * * @param argc The total number of arguments * @param argv An array of 'C' strings containing the arguments * * @return error code (<tt>ALL_OK</tt> if successful) */static MIDPErrorsetupArgToStartMidlet(int argc, char* argv[]) {    MIDPError status = BAD_PARAMS;    SuiteIdType tmpSuiteId = UNUSED_SUITE_ID;    if (argc == 0) {        return status;    }    do {        int i, len;        /* if the storage name only digits, convert it */        int onlyDigits = 1;        len = strlen(argv[0]);        for (i = 0; i < len; i++) {            if (!isdigit((argv[0])[i])) {                onlyDigits = 0;                break;            }        }        if (onlyDigits) {            /* run the midlet suite by its ID */            int i;            /* the format of the string is "number:" */            if (sscanf(argv[0], "%d", &tmpSuiteId) != 1) {                REPORT_ERROR(LC_AMS, "Invalid suite ID format");                fprintf(stderr, "Invalid suite ID format\n");                break;            }            for (i = 0; i < numberOfSuiteIds; i++) {                if (tmpSuiteId == pSuiteIds[i]) {                    break;                }            }            if (i == numberOfSuiteIds) {                REPORT_ERROR(LC_AMS, "Suite with the given ID was not found");                fprintf(stderr, "Suite with the given ID was not found\n");                break;            }            suiteIDToRun = tmpSuiteId;        } else {            /* Run by ID */            suiteIDToRun = INTERNAL_SUITE_ID;        }        /* Setting up a class name of the midlet to be run */        if (argc > 1) {            if (PCSL_STRING_OK !=                pcsl_string_from_chars(argv[1], &classNameToRun)) {                status = OUT_OF_MEMORY;                break;            }        }        /* Setting up arguments for the midlet */        for (i = 0; i < 3; i ++) {            if (argc > i + 2) {                if (PCSL_STRING_OK !=                    pcsl_string_from_chars(argv[i + 2], &argsForMidlet[i])) {                    status = OUT_OF_MEMORY;                    break;                }            } else {                argsForMidlet[i] = PCSL_STRING_NULL;            }        }        if (pcsl_string_is_null(&classNameToRun)) {            int res = find_midlet_class(suiteIDToRun, 1, &classNameToRun);            if (OUT_OF_MEM_LEN == res) {                status = OUT_OF_MEMORY;                break;            }            if (NULL_LEN == res) {                REPORT_ERROR(LC_AMS, "Could not find the first MIDlet");                fprintf(stderr, "Could not find the first MIDlet\n");                break;            }        }        status = ALL_OK;    } while (0);    if (status == OUT_OF_MEMORY) {        REPORT_ERROR(LC_AMS, "Out of Memory");        fprintf(stderr, "Out Of Memory\n");    }    return status;}static MIDPError runMidletWithNAMS(int argc, char* argv[]);/** * Mode 1. NAMS test service:<br> * runNams [&lt;VM args&gt;] -namsTestService<br> * Does not return until the system is stopped. * * @param argc The total number of arguments * @param argv An array of 'C' strings containing the arguments * * @return error code (<tt>ALL_OK</tt> if successful) */static MIDPError runNamsTestService(int argc, char* argv[]) {    int numOfArgsToRunTestService = 2;    char* ppArgsToRunTestService[] = {        "-1", /* internal suite id */        NAMS_TEST_SERVICE_CLASS_NAME    };#if ENABLE_I3_TEST    initNams();    initNamsCommands(argc - 1, argv + 1);#else    (void)argc;    (void)argv;#endif    return runMidletWithNAMS(numOfArgsToRunTestService,                             ppArgsToRunTestService);}/** * Mode 2. Run a MIDlet using the NAMS API:<br> * runNams [&lt;VM args&gt;] -runMidlet &lt;suiteId or suite number&gt;<br> *   &lt;MIDlet classname&gt; [[[&lt;arg1&gt;] &lt;arg2&gt;] &lt;arg3&gt;]<br> * * @param argc The total number of arguments * @param argv An array of 'C' strings containing the arguments * * @return error code (<tt>ALL_OK</tt> if successful) */static MIDPError runMidletWithNAMS(int argc, char* argv[]) {    MIDPError status;    status = setupArgToStartMidlet(argc, argv);    if (status != ALL_OK) {        return status;    }    /* set the listeners before starting the system */    midp_add_event_listener(system_state_listener, SYSTEM_EVENT_LISTENER);    midp_add_event_listener(background_listener, DISPLAY_EVENT_LISTENER);    midp_add_event_listener(foreground_listener, DISPLAY_EVENT_LISTENER);    midp_add_event_listener(state_change_listener, MIDLET_EVENT_LISTENER);    return midp_system_start();}/** * Mode 3. Run a MIDlet in the AMS isolate using the JAMS mode API:<br> * runNams [&lt;VM args&gt;] -jamsTestMode &lt;suiteId or suite number&gt;<br> *     &lt;MIDlet classname&gt; [[[&lt;arg1&gt;] <arg2>] &lt;arg3&gt;]<br> * * @param argc The total number of arguments * @param argv An array of 'C' strings containing the arguments * * @return <tt>ALL_OK</tt> if successful, *         <tt>BAD_PARAMS</tt> if some parameter is invalid, *         <tt>NOT_FOUND</tt> if the MIDlet suite not found, *         <tt>GENERAL_ERROR</tt> if another error */static MIDPError runMidletWithJAMS(int argc, char* argv[]) {    int retCode = MIDP_ERROR_STATUS;    MIDPError status;    if (argc < 1) {        return BAD_PARAMS;    }    status = setupArgToStartMidlet(argc, argv);    if (status != ALL_OK) {        return status;    }    retCode = midp_run_midlet_with_args_cp(suiteIDToRun, &classNameToRun,        &argsForMidlet[0], &argsForMidlet[1], &argsForMidlet[2], 0, NULL);    /*     * IMPL_NOTE: this code can be removed after migration of the status codes     * returned from midp_run_midlet_with_args_cp() to MIDPError.     */    if (retCode == 0 || retCode == MIDP_SHUTDOWN_STATUS) {        status = ALL_OK;    } else if (status == SUITE_NOT_FOUND_STATUS) {        status = NOT_FOUND;    } else if (status != ALL_OK) {        status = GENERAL_ERROR;    }    return status;}/** * Mode 4. Run an alternate main class:<br> * runNams [&lt;VM args&gt;] -runMainClass mainClass [&lt;args&gt;] * * @param argc The total number of arguments * @param argv An array of 'C' strings containing the arguments * * @return <tt>ALL_OK</tt> if successful, *         <tt>BAD_PARAMS</tt> if some parameter is invalid, *         <tt>GENERAL_ERROR</tt> if another error */static MIDPError runMainClass(int argc, char* argv[]) {    char* mainClass; /* the class name of midlet to run */    int retCode = MIDP_ERROR_STATUS;    MIDPError status = ALL_OK;    if (argc < 1) {        return BAD_PARAMS;    }    if (midpInitialize() != 0) {        return OUT_OF_MEMORY;    }    mainClass = argv[0];    retCode = midpRunMainClass(NULL, mainClass, argc, argv);    midpFinalize();    if (retCode == 0 || retCode == MIDP_SHUTDOWN_STATUS) {        status = ALL_OK;    } else if (retCode != ALL_OK) {        status = GENERAL_ERROR;    }    return status;}/** * Start the MT MIDP. Waits until it shuts down, and then exits by default. * If the -restart option is given, and no VM error occurred, MIDP is * restarted. * * @param argc The total number of arguments * @param argv An array of 'C' strings containing the arguments * * @return <tt>0</tt> for success, otherwise <tt>-1</tt> */int runNams(int argc, char* argv[]) {    MIDPError status;    int used;    int savedArgc;    char **savedArgv;    int restart = 0;    savedArgc = argc;    savedArgv = argv;    /* initialize the system */    status = midp_system_initialize();    if (status != ALL_OK) {        fprintf(stderr, "midp_system_initialize() failed (%d)\n", status);        return status;    }    do {        argc = savedArgc;        argv = savedArgv;        /* Parse VM arguments */        argc--;        argv++;        while ((used = JVM_ParseOneArg(argc, argv)) > 0) {            argc -= used;            argv += used;        }        /*         * Parse runNams arguments. The following options are allowed:         *         * -namsTestService         * -runMidlet <suiteId or suite number> <MIDlet classname>         *     [[[<arg1>] <arg2>] <arg3>]         * -jamsTestMode <suiteId or suite number> <MIDlet classname>         *     [[[<arg1>] <arg2>] <arg3>]         * -runMainClass mainClass [<args>]         */        if (argc > 0 && 0 == strcmp(argv[0], "-restart")) {            restart = 1;            argc--;            argv++;        }        if (argc < 1) {            fprintf(stderr, runUsageText);            break;        } else {            int i;            int mode = -1;            char *options[] = {                "-namsTestService", "-runMidlet",                "-jamsTestMode", "-runMainClass"            };            MIDPError (*handlers[])(int argc, char* argv[]) = {                runNamsTestService, runMidletWithNAMS,                runMidletWithJAMS, runMainClass            };            for(i = 0; i < (int)(sizeof(options) / sizeof(options[0])); i++) {                if (!strcmp(argv[0], options[i])) {                    mode = i;                    break;                }            }            if (mode == -1) {                fprintf(stderr, runUsageText);                break;            } else {                /* load the suite id's */                status = loadSuiteIds();                if (status != ALL_OK) {                    fprintf(stderr, "Failed to load suite IDs (%d)\n", status);                    break;                }                status = handlers[i](--argc, ++argv);            }        }        /* clean up */        unloadSuiteIds();        if (status != ALL_OK) {            fprintf(stderr, "VM startup failed (%d)\n", status);            break;        }    } while (restart);    /* it is safe to call it more than once */    unloadSuiteIds();    return status;}

⌨️ 快捷键说明

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