#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <mtd/mtd-user.h>
#include "hal.h"
#include "ratelimit.h"
#include "hal_interface_flash.h"

#define TAG "TAG_FLASH"
#include "hlog.h"





/*flash init interface define,should be implement if needed*/
uint32_t hal_flash_init(){
	return 0;
}

/*flash deinit interface define,should be implement if needed*/
uint32_t hal_flash_deinit(){
	return 0;
}

uint32_t hal_flash_read(const int8_t *path, int8_t *data, uint32_t max_size){
    FILE *file = fopen ( path, "r" );
    if ( file ) {
        uint32_t readed = fread( data, 1, max_size, file );
        fclose(file);
        return readed;
    }
	return 0;
}

uint32_t hal_flash_write(const int8_t *path, int8_t *data, uint32_t size){
    FILE *file = fopen ( path, "w" );
    if ( file ) {
        uint32_t writed = fwrite( data, 1, size, file );
        CHECK_SAME(writed, size);
        fclose(file);
        return writed;
    }
    CHECK_NULL(path, file);
	return 0;
}

uint32_t hal_flash_test(){
    char path[MAX_PATH];
    char buf[MAX_PATH];
    sprintf(path, "%s/%s_test.txt", (char *)get_current_dir_name(), get_filename(__FILE__));
    hal_flash_write(path, "1234560", 6);
    uint32_t size = hal_flash_read(path, buf, sizeof(buf));
    hlogd("read:%u, str:%s", size, buf );
    return 0;
}



