Ejemplo n.º 1
0
KNIEXPORT KNI_RETURNTYPE_VOID
KNIDECL(com_sun_midp_main_CDCInit_initMidpNativeStates) {
    jchar jbuff[1024];
    char cbuff[1024];
    int max = sizeof(cbuff) - 1;
    int len, i;

    KNI_StartHandles(1);
    KNI_DeclareHandle(home);
    KNI_GetParameterAsObject(1, home);

    len = KNI_GetStringLength(home);
    if (len > max) {
        len = max;
    }

    KNI_GetStringRegion(home, 0, len, jbuff);
    for (i=0; i<len; i++) {
        cbuff[i] = (char)jbuff[i];
    }
    cbuff[len] = 0;

    initCDCEvents();

    midpSetHomeDir(cbuff);
    if (midpInitialize() != 0) {
        printf("midpInitialize() failed\n");

    }

    if (midpInitCallback(VM_LEVEL, midpInitializeUI, midpFinalizeUI) != 0) {
        printf("midpInitCallback(VM_LEVEL, ...) failed\n");
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Ejemplo n.º 2
0
/**
 * Lists all installed MIDlet suites. This is an example of how to use
 * the public MIDP API.
 *
 * @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>
 *
 * IMPL_NOTE: determine if it is desirable for user targeted output
 *       messages to be sent via the log/trace service, or if
 *       they should remain as printf calls
 */
int
listMidlets(int argc, char* argv[]) {
    int   status = -1;
    int   i;
    long  size;
    char* midpHome = NULL;

    (void)argv;                                   /* Avoid compiler warnings */
    if (argc > 1) {
        REPORT_ERROR(LC_AMS, "Too many arguments given");
        fprintf(stderr, "Too many arguments given\n");
        return -1;
    }

    /* get midp home directory, set it */
    midpHome = midpFixMidpHome(argv[0]);
    if (midpHome == NULL) {
        return -1;
    }
    /* set up midpHome before calling initialize */
    midpSetHomeDir(midpHome);

    if (midpInitialize() != 0) {
        REPORT_ERROR(LC_AMS, "Not enough memory");
        fprintf(stderr, "Not enough memory\n");
        return -1;
    }

    do {
        SuiteIdType* pSuites = NULL;
        int numberOfSuites = 0;
        MIDPError err;

        err = midp_get_suite_ids(&pSuites, &numberOfSuites);
        if (err != ALL_OK) {
            REPORT_ERROR1(LC_AMS, "Error in midp_get_suite_ids(), code %d",
                          err);
            fprintf(stderr, "Error in midp_get_suite_ids(), code %d.\n", err);
            break;
        }

        if (numberOfSuites == 0) {
            REPORT_ERROR(LC_AMS, "No MIDlet Suites installed on phone");
            printf("** No MIDlet Suites installed on phone\n");
            status = 0;
            break;
        }

        for (i = 0; i < numberOfSuites; i++) {
            MidpInstallInfo info;
            MidpProperties properties;

            info = midp_get_suite_install_info(pSuites[i]);
            if (BAD_ID_INFO_STATUS(info)) {
                REPORT_ERROR(LC_AMS, "Suite list is corrupt");
                fprintf(stderr, "Suite list is corrupt\n");
                break;
            }

            if (OUT_OF_MEM_INFO_STATUS(info)) {
                REPORT_ERROR(LC_AMS, "Out Of Memory for Info");
                fprintf(stderr, "Out Of Memory for Info\n");
                break;
            }

            if (SUITE_CORRUPTED_ERR_STATUS(info)) {
                /*
                 * Installinfo is not initialsed in case of an error
                 * so no need to free it
                 */
                REPORT_ERROR1(LC_AMS, "Error : Suite %d is corrupted", (i+1));
                fprintf(stderr, "Error : Suite %d is corrupted\n", (i+1));
                continue;
            }

            if (READ_ERROR_INFO_STATUS(info)) {
                REPORT_ERROR(LC_AMS, "Corrupt install info");
                fprintf(stderr, "Corrupt install info\n");
                break;
            }

            properties = midp_get_suite_properties(pSuites[i]);
            if (OUT_OF_MEM_PROPERTY_STATUS(properties)) {
                midp_free_install_info(&info);
                midp_free_properties(&properties);
                REPORT_ERROR(LC_AMS, "Out Of Memory for properties");
                fprintf(stderr, "Out Of Memory for properties\n");
                break;
            }

            if (CORRUPTED_PROPERTY_STATUS(properties)) {
                midp_free_install_info(&info);
                midp_free_properties(&properties);
                REPORT_ERROR1(LC_AMS, "Error : Suite %d is corrupted", (i+1));
                fprintf(stderr, "Error : Suite %d is corrupted\n", (i+1));
                continue;
            }

            if (READ_ERROR_PROPERTY_STATUS(properties)) {
                midp_free_install_info(&info);
                midp_free_properties(&properties);
                REPORT_ERROR(LC_AMS, "Corrupt properties");
                fprintf(stderr, "Corrupt properties\n");
                break;
            }

            printf("[%d]\n", (i + 1));
            printProperty("  Name: ", &SUITE_NAME_PROP, properties);
            printProperty("  Vendor: ", &SUITE_VENDOR_PROP, properties);
            printProperty("  Version: ", &SUITE_VERSION_PROP, properties);
            printProperty("  Description: ", &SUITE_DESC_PROP, properties);

            if (info.authPathLen > 0) {
                int j;

                puts("  Authorized by: ");
                for (j = 0; j < info.authPathLen; j++) {
                    print_field("    ", &info.authPath_as[j]);
                }
            }

            print_field("  SecurityDomain: ", &info.domain_s);
            printf("  Verified: %s\n",
                (info.pVerifyHash != NULL ? "true" : "false"));
            printf("  Suite ID: %ld\n", (long)pSuites[i]);
            print_field("  JAD URL: ", &info.jadUrl_s);
            print_field("  JAR URL: ", &info.jarUrl_s);
            size = midp_get_suite_storage_size(pSuites[i]);
            if (size < 0) {
                fprintf(stderr, "Ran out of memory getting the size\n");
            } else {
                printf("  Size: %ldK\n", (size + 1023) / 1024);
            }

            midp_free_install_info(&info);
            midp_free_properties(&properties);
        }

        midp_free_suite_ids(pSuites, numberOfSuites);

        status = 0;
    } while (0);

    midpFinalize();

    return status;
}
Ejemplo n.º 3
0
/**
 * Runs a MIDlet from an installed MIDlet suite. This is an example of
 * how to use the public MIDP API.
 *
 * @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>
 *
 * IMPL_NOTE:determine if it is desirable for user targeted output
 *       messages to be sent via the log/trace service, or if
 *       they should remain as printf calls
 */
int
runMidlet(int argc, char** commandlineArgs) {
    int status = -1;
    SuiteIdType suiteId   = UNUSED_SUITE_ID;
    pcsl_string classname = PCSL_STRING_NULL;
    pcsl_string arg0 = PCSL_STRING_NULL;
    pcsl_string arg1 = PCSL_STRING_NULL;
    pcsl_string arg2 = PCSL_STRING_NULL;
    int repeatMidlet = 0;
    char* argv[RUNMIDLET_MAX_ARGS];
    int i, used;
    int debugOption = MIDP_NO_DEBUG;
    char *progName = commandlineArgs[0];
    char* midpHome = NULL;
    char* additionalPath;
    SuiteIdType* pSuites = NULL;
    int numberOfSuites = 0;

    JVM_Initialize(); /* It's OK to call this more than once */

    /*
     * Set Java heap capacity now so it can been overridden from command line.
     */
    JVM_SetConfig(JVM_CONFIG_HEAP_CAPACITY, MIDP_HEAP_REQUIREMENT);

    /*
     * Parse options for the VM. This is desirable on a 'development' platform
     * such as linux_qte. For actual device ports, copy this block of code only
     * if your device can handle command-line arguments.
     */

    /* JVM_ParseOneArg expects commandlineArgs[0] to contain the first actual
     * parameter */
    argc --;
    commandlineArgs ++;

    while ((used = JVM_ParseOneArg(argc, commandlineArgs)) > 0) {
        argc -= used;
        commandlineArgs += used;
    }

    /* Restore commandlineArgs[0] to contain the program name. */
    argc ++;
    commandlineArgs --;
    commandlineArgs[0] = progName;

    /*
     * Not all platforms allow rewriting the command line arg array,
     * make a copy
     */
    if (argc > RUNMIDLET_MAX_ARGS) {
        REPORT_ERROR(LC_AMS, "Number of arguments exceeds supported limit");
        fprintf(stderr, "Number of arguments exceeds supported limit\n");
        return -1;
    }
    for (i = 0; i < argc; i++) {
        argv[i] = commandlineArgs[i];
    }

    if (midpRemoveOptionFlag("-debug", argv, &argc) != NULL) {
        debugOption = MIDP_DEBUG_SUSPEND;
    }

    if (midpRemoveOptionFlag("-loop", argv, &argc) != NULL) {
        repeatMidlet = 1;
    }

    /* additionalPath gets appended to the classpath */
    additionalPath = midpRemoveCommandOption("-classpathext", argv, &argc);

    if (argc == 1) {
        REPORT_ERROR(LC_AMS, "Too few arguments given.");
        fprintf(stderr, runUsageText);
        return -1;
    }

    if (argc > 6) {
        REPORT_ERROR(LC_AMS, "Too many arguments given\n");
        fprintf(stderr, "Too many arguments given\n%s", runUsageText);
        return -1;
    }

    /* get midp home directory, set it */
    midpHome = midpFixMidpHome(argv[0]);
    if (midpHome == NULL) {
        return -1;
    }
    /* set up midpHome before calling initialize */
    midpSetHomeDir(midpHome);

    if (midpInitialize() != 0) {
        REPORT_ERROR(LC_AMS, "Not enough memory");
        fprintf(stderr, "Not enough memory\n");
        return -1;
    }

    do {
        int onlyDigits;
        int len;
        int i;

        if (argc > 5) {
            if (PCSL_STRING_OK != pcsl_string_from_chars(argv[5], &arg2)) {
                REPORT_ERROR(LC_AMS, "Out of Memory");
                fprintf(stderr, "Out Of Memory\n");
                break;
            }
        }

        if (argc > 4) {
            if (PCSL_STRING_OK != pcsl_string_from_chars(argv[4], &arg1)) {
                REPORT_ERROR(LC_AMS, "Out of Memory");
                fprintf(stderr, "Out Of Memory\n");
                break;
            }
        }

        if (argc > 3) {
            if (PCSL_STRING_OK != pcsl_string_from_chars(argv[3], &arg0)) {
                REPORT_ERROR(LC_AMS, "Out of Memory");
                fprintf(stderr, "Out Of Memory\n");
                break;
            }
        }

        if (argc > 2) {
            if (PCSL_STRING_OK != pcsl_string_from_chars(argv[2], &classname)) {
                REPORT_ERROR(LC_AMS, "Out of Memory");
                fprintf(stderr, "Out Of Memory\n");
                break;
            }

        }

        /* if the storage name only digits, convert it */
        onlyDigits = 1;
        len = strlen(argv[1]);
        for (i = 0; i < len; i++) {
            if (!isdigit((argv[1])[i])) {
                onlyDigits = 0;
                break;
            }
        }

        if (onlyDigits) {
            /* Run by number */
            int suiteNumber;
            MIDPError err;

            /* the format of the string is "number:" */
            if (sscanf(argv[1], "%d", &suiteNumber) != 1) {
                REPORT_ERROR(LC_AMS, "Invalid suite number format");
                fprintf(stderr, "Invalid suite number format\n");
                break;
            }

            err = midp_get_suite_ids(&pSuites, &numberOfSuites);
            if (err != ALL_OK) {
                REPORT_ERROR1(LC_AMS, "Error in midp_get_suite_ids(), code %d",
                              err);
                fprintf(stderr, "Error in midp_get_suite_ids(), code %d.\n",
                        err);
                break;
            }

            if (suiteNumber > numberOfSuites || suiteNumber < 1) {
                REPORT_ERROR(LC_AMS, "Suite number out of range");
                fprintf(stderr, "Suite number out of range\n");
                midp_free_suite_ids(pSuites, numberOfSuites);
                break;
            }

            suiteId = pSuites[suiteNumber - 1];
        } else {
            /* Run by ID */
            suiteId = INTERNAL_SUITE_ID;

            /* IMPL_NOTE: consider handling of other IDs. */

            if (strcmp(argv[1], "internal") && additionalPath == NULL) {
                /*
                 * If the argument is not a suite ID, it might be a full
                 * path to the midlet suite's jar file.
                 * In this case this path is added to the classpath and
                 * the suite is run without installation (it is useful
                 * for internal test and development purposes).
                 */
                additionalPath = argv[1];
            }
        }

        if (pcsl_string_is_null(&classname)) {
            int res = find_midlet_class(suiteId, 1, &classname);
            if (OUT_OF_MEM_LEN == res) {
                REPORT_ERROR(LC_AMS, "Out of Memory");
                fprintf(stderr, "Out Of Memory\n");
                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;
            }
        }

        do {
            status = midp_run_midlet_with_args_cp(suiteId, &classname,
                                             &arg0, &arg1, &arg2,
                                             debugOption, additionalPath);
        } while (repeatMidlet && status != MIDP_SHUTDOWN_STATUS);

        if (pSuites != NULL) {
            midp_free_suite_ids(pSuites, numberOfSuites);
            suiteId = UNUSED_SUITE_ID;
        }
    } while (0);

    pcsl_string_free(&arg0);
    pcsl_string_free(&arg1);
    pcsl_string_free(&arg2);
    pcsl_string_free(&classname);

    switch (status) {
    case MIDP_SHUTDOWN_STATUS:
        break;

    case MIDP_ERROR_STATUS:
        REPORT_ERROR(LC_AMS, "The MIDlet suite could not be run.");
        fprintf(stderr, "The MIDlet suite could not be run.\n");
        break;

    case SUITE_NOT_FOUND_STATUS:
        REPORT_ERROR(LC_AMS, "The MIDlet suite was not found.");
        fprintf(stderr, "The MIDlet suite was not found.\n");
        break;

    default:
        break;
    }

    midpFinalize();

    return status;
}