//=============================================================================================================
#include <stdint.h>
#include <stdlib.h>
#include <nfp/me.h>
#include <nfp/mem_atomic.h>
#include <pif_common.h>
#include "pif_plugin.h"

//=============================================================================================================
extern void function_48725(PIF_PLUGIN_map_hdr_T *mapHdr, PIF_PLUGIN_udp_T *udpHdr);

void function_1(PIF_PLUGIN_map_hdr_T *mapHdr, PIF_PLUGIN_udp_T *udpHdr)
{
    __gpr uint32_t i = 1;
    unsigned randval;
    __gpr uint32_t calc_prime = 1;
    short b = 0;
    // for (;i < 1500000; i++);
        // __gpr uint32_t i = 1;
    uint32_t prime = 100;
    uint32_t j = 2;
    uint32_t x = 1;

    for (;i < prime; i++) {
        b = 0;
        for (j = 2; j <= (i / 2); j++)  {
            if (i % j == 0) {
                b = 1;
                break;
            }
        }
        if (b == 0)
            calc_prime = i;
    }
    mapHdr->function_id += 3;
    // PIF_PLUGIN_map_hdr_T *mapHdr = pif_plugin_hdr_get_map_hdr(headers);
    
    randval=local_csr_read(local_csr_pseudo_random_number);
    i = randval % (10);
    udpHdr->dstPort = 20000+i;
}

void default_function(PIF_PLUGIN_map_hdr_T *mapHdr)
{
    return ;
}
// __export __gpr uint8_t packet_count=0;
__export __emem uint8_t packet_count;

int pif_plugin_static_dispatch_function(EXTRACTED_HEADERS_T *headers, MATCH_DATA_T *match_data) {

    // PIF_PLUGIN_ethernet_T *ipv4_hdr = pif_plugin_hdr_get_ethernet(headers);
    // PIF_PLUGIN_ipv4_T *mapHdr = pif_plugin_hdr_get_ipv4(headers);
    // PIF_PLUGIN_udp_T *mapHdr = pif_plugin_hdr_get_udp(headers);

    PIF_PLUGIN_map_hdr_T *mapHdr = pif_plugin_hdr_get_map_hdr(headers);
    PIF_PLUGIN_udp_T *udpHdr = pif_plugin_hdr_get_udp(headers);
 
    // __gpr uint32_t i = 1, j = 0;
    
   
        uint32_t fid = mapHdr->function_id;
        if(fid==1)
        {
            function_1(mapHdr,udpHdr);
        }
        if(fid==48725)
        {
            function_48725(mapHdr,udpHdr);
        }
        else{
            default_function(mapHdr);
        }
        // mapHdr->data = 0;
    
    // else if(packet_count>16){
        // packet_count= packet_count%16;
        // mapHdr->data = 21;
    // }
    // else{
    //     mapHdr->data = 21;
    // }
    // mapHdr->function_id=packet_count;
    return PIF_PLUGIN_RETURN_FORWARD; 
}
